문제

If I have a function that evaluates to a function

(defn func1 [c1 c2]
  (fn [x1 x2]
    ...do some stuff with c1 c2 x1))

that I use elsewhere in a map or reduce, is it better to use inline

(defn func2 [x y z]
  (reduce (func1 x y) z (range 20)))

or to let bind it first

(defn func2 [x y z]
  (let [ffunc (func1 x y)]
    (reduce ffunc z (range 20))))

In the first case I would be worried that a new function over x and y is generated each step through the reduce.

도움이 되었습니까?

해결책

The evaluation of the function call (func1 x y) is done once in each case.

The rule for evaluating a function call in Clojure consists of evaluating all the expressions that are provided as its arguments and then invoking the function with those values.

If you define the following higher order function:

(defn plus []
  (println "calling plus")
  +)

And then call reduce in the following way:

(reduce (plus) [0 1 2 3])

A single calling plus is printed, showing the function plus is invoked only once.

The same thing happens when using the let form:

(let [f (plus)]
  (reduce f [0 1 2 3]))

Hope it helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top