如何获得一个调用Java方法的Fn? [英] How to get an Fn which calls a Java method?
问题描述
我正在学习Clojure。我写这个代码递归地走一个目录。
I'm learning Clojure. I wrote this code to recursively walk a directory.
(tree-seq #(.isDirectory %1) #(.listFiles %1) (File. "/my-directory"))
为什么不能使用 .isDirectory
作为Clojure中的第一类函数?有没有更好的方法来重写这个代码?
Why can't I use .isDirectory
as a first-class function in Clojure? Is there a better way to rewrite this code?
推荐答案
Java方法不是clojure函数,因为你不能调用方法在其自己的;你必须在一个对象上调用一个方法,它必须是该方法所期望的类型的对象。换句话说,在java中,一个方法不能与其定义类完全分离(至少不能有效地)。
Java methods aren't clojure functions because you can't call a method on its own; you have to call a method on an object, and it has to be an object of the type that the method expects. In other words, in java, a method cannot be fully separated from its defining class (at least not efficiently).
#(。foo%)的替代方法是(memfn foo),几乎没有人使用#(...)后引入。
An alternative to #(.foo %) would be (memfn foo), which hardly anyone uses anymore after #(...) was introduced.
这篇关于如何获得一个调用Java方法的Fn?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!