Usage of java methods as a function argument in Clojure

前端 未结 1 386
深忆病人
深忆病人 2021-01-21 07:37

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         


        
相关标签:
1条回答
  • 2021-01-21 08:07

    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)
    
    0 讨论(0)
提交回复
热议问题