How to get an Fn which calls a Java method?

后端 未结 4 1336
梦谈多话
梦谈多话 2021-01-20 22:33

I\'m learning Clojure. I wrote this code to recursively walk a directory.

(tree-seq #(.isDirectory %1) #(.listFiles %1) (File. \"/my-directory\"))

4条回答
  •  既然无缘
    2021-01-20 23:07

    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).

    An alternative to #(.foo %) would be (memfn foo), which hardly anyone uses anymore after #(...) was introduced.

提交回复
热议问题