将Clojure函数作为java.util.Function传递 [英] Passing a Clojure function as java.util.Function
本文介绍了将Clojure函数作为java.util.Function传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
就像在主题中一样,我想使用一种Java方法,以一个Function作为参数,并为其提供一个Clojure
函数,无论是匿名函数还是常规函数.有人知道该怎么做吗?
As in topic, I'd like to use a Java method taking a Function as an argument and provide it with a Clojure
function, be it anonymous or a regular one. Anyone has any idea how to do that?
推荐答案
java.util.function.Function
是接口.
您需要实现抽象方法 apply(T t)
.
像这样的事情应该做到这一点:
java.util.function.Function
is an interface.
You need to implement the abstract method apply(T t)
.
Something like this should do it:
(defn hello [name]
(str "Hello, " name "!"))
(defn my-function[]
(reify
java.util.function.Function
(apply [this arg]
(hello arg))))
;; then do (my-function) where you need to pass in a Function
这篇关于将Clojure函数作为java.util.Function传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文