I am still going through Clojure For the Brave and True. He does some pretty mind-bending things in chapter 5 when he walks you through his pegthing program.
Check this out:
(defn add-pos "Pegs the position and performs connections" [board max-pos pos] (let [pegged-board (assoc-in board [pos :pegged] true)] (reduce (fn [new-board connection-creation-fn] (connection-creation-fn new-board max-pos pos)) pegged-board [connect-right connect-down-left connect-down-right])))
In the call to “reduce”, he is sending it a collection of functions called “connect-right”, “connect-down-left” and “connect-down-right”. Then in the function in the reduce, there is a call to “connection-creation-fn”, which is passed to the anonymous function. So he is not actually calling a function called “connection-creation-fn”; that is a placeholder for the functions in the array being passed to “reduce”.
Granted, I knew before this what “reduce” is. But in this chapter he has a lot of functions calling functions. Granted, he does say that reducing over a collection of functions is not something you will do or see very often. Even though I have been looking at Clojure for a while, and this chapter wasn’t really anything new, it was a reminder how different functional programming can be.