Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map over sequence of Java methods to be executed

Tags:

clojure

The following will work:

(map #(%1 "21") [identity])

However the following code fails

(map #(%1 "21") [.toString])

How do I create a seq/collection of Java methods?

like image 646
murtaza52 Avatar asked Mar 05 '13 07:03

murtaza52


1 Answers

Java methods are not first class citizens like functions in clojure. You can't pass java methods as parameters, return or store in variables and collections. But you can create function by memfn or just anonymous function which wraps original method call

(map #(%1 21) [(memfn toString)])
=> ("21")

(map #(%1 21) [#(.toString %)])
=> ("21")
like image 85
mobyte Avatar answered Nov 15 '22 10:11

mobyte