clojure java math,在Clojure中使用java方法作为函数参数

How can I use java methods as a functions arguments in Clojure?

For example, I want to make a functions composition:

user> (Integer. (str \9))

9

user> ((comp Integer. str) \9)

CompilerException java.lang.ClassNotFoundException: Integer., compiling:(NO_SOURCE_PATH:1:2)

That does not work.

memfn doesn't help also:

user> (map (comp (memfn Integer.) str) "891")

IllegalArgumentException No matching method found: Integer. for class java.lang.String clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

Any ideas?

Related questions (that, though, do not give the right answer to the question):

(Note: it seems to be that the answer suggested by dave, using of an anonymous function as a wrapper, is the best solution)

解决方案

Unlike Clojure functions, Java methods weren't designed to be first class. When you use Java inter-op in Clojure, you're literally working with Java methods, so you don't get the added benefits that were implemented for Clojure functions. For more info, see the comments below.

As a workaround to use Java methods as arguments, you can wrap them in an anonymous function, like this, effectively making them Clojure functions:

((comp #(Integer. %) str) \9)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值