Calling Clojure Higher Order Functions

If I define a function that returns such a function:

(defn add-n [n] (fn [x] (+ xn))) 

Then I can assign the result to the symbol:

 (def add-1 (add-n 1)) 

and name it:

 (add-1 41) ;=> 42 

How can I call the result (add-n 1) without assigning it a new character? The following produces this conclusion:

 (println (add-n 1)) #<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5 > nil 

#<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5 > is an internal reference to the generated function.

+7
source share
1 answer

Easy:

 (println ((add-n 1) 41)) 

The result you choose is a function definition. Putting between parentheses and adding a parameter is enough to call it.

+16
source

All Articles