开发者

Calling Clojure higher-order functions

开发者 https://www.devze.com 2023-03-05 20:12 出处:网络
If I define a function that returns a function like this: (defn add-n [n] (fn [x] (+ x n))) I can then assign the result to a symbol:

If I define a function that returns a function like this:

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

I can then assign the result to a symbol:

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

and call it:

(add-1 41)
;=> 42

How do I call the result of (add-n 1) without assigning it to a new symbol? The following produces this output:

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

The #<user$add_n$fn__33 user$add_n$fn__33@e9ac0f5&g开发者_如何学JAVAt; is an internal reference to the generated function.


Easy:

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

The output you saw is a function definition. Putting it between round brackets and adding a parameter is enough to call it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号