Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clojure: (apply fn coll) vs (apply #(apply fn %&) coll)

Tags:

clojure

I am working my way through labrepl and I saw some code that follows this pattern:

;; Pattern
(apply #(apply f %&) coll)

;; Concrete example
user=> (apply #(apply + %&) [1 2 3 4])
10

This seems to be equivalent to this pattern:

;; Pattern
(apply f coll)

;; Concrete example
user=> (apply + [1 2 3 4]) 
10

Are these patterns equivalent? If not, what's the difference and when would you use one over the other?

I took the former pattern from the step function in the cellular-automata lab of labrepl:

(defn step
  "Advance the automation by one step, updating all cells."
  [board]
  (doall
   (map (fn [window]
          (apply #(apply map brians-brain-rules %&)
                 (doall (map torus-window window))))
        (torus-window board))))

Update: I added a concrete example of each pattern to help make the question clearer.

like image 302
rigdern Avatar asked Oct 07 '12 00:10

rigdern


1 Answers

No, there is no difference. There is no reason to write the longer form; I can only assume it was arrived at by gradual changes to code that made sense at one time.

like image 185
amalloy Avatar answered Nov 10 '22 14:11

amalloy