在map函数中使用interop构造函数(Clojure) [英] Using interop constructor in map function(Clojure)

查看:85
本文介绍了在map函数中使用interop构造函数(Clojure)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Integer构造函数映射函数时收到此投诉:

  => (map Integer。[123])
CompilerException java.lang.ClassNotFoundException:Integer。,compiling:(NO_SOURCE_PATH:1:1)



然而,当我在一个函数中构造函数时,一切正常:

  => (defn str-to-int [str](Integer.str))
=> (map str-to-int [123])
(1 2 3)

为什么要在另一个函数中换行整数,使这个工作?有没有更好的方法使其工作,而不创建额外的功能?

解决方案

map接收一个函数,interop使用一种特殊的形式,如new。 ..
用匿名函数文字包装这些是很容易的



例如

 (map#(Integer。%)[123])

产生所需的结果。


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)

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 takes in a function and interop uses a special forms like new . and .. It is fairly easy to wrap these with anonymous function literals

for example

(map #(Integer. %) ["1" "2" "3"])

produces the desired result.

这篇关于在map函数中使用interop构造函数(Clojure)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆