在映射函数中使用互操作构造函数(Clojure) [英] Using interop constructor in map function(Clojure)
本文介绍了在映射函数中使用互操作构造函数(Clojure)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将整数构造函数传递给映射函数时收到此投诉:
I am getting this complaint when passing Integer constructor to map function :
=> (map Integer. ["1" "2" "3"])
CompilerException java.lang.ClassNotFoundException: Integer., compiling:(NO_SOURCE_PATH:1:1)
但是,当我将构造函数包装在一个函数中时,一切正常:
However when I wrap the constructor in a function everything works:
=> (defn str-to-int [str] (Integer. str))
=> (map str-to-int ["1" "2" "3"])
(1 2 3)
为什么我必须将 Integer 包装在另一个函数中才能使其工作?有没有更好的方法让它在不创建附加功能的情况下工作?
Why do I have to wrap Integer in another function to make this work? Is there a better way to make it work without creating additional function?
推荐答案
map 接受一个函数,interop 使用一个特殊的形式,比如 new .和 ..用匿名函数字面量包装这些是相当容易的
map takes in a function and interop uses a special forms like new . and .. It is fairly easy to wrap these with anonymous function literals
例如
(map #(Integer. %) ["1" "2" "3"])
产生想要的结果.
这篇关于在映射函数中使用互操作构造函数(Clojure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文