Higher order functions in Clojure support chaining

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com


One easy-to-miss strength of this approach is that it supports chaining. In a few places in our application, we wanted to send a computation to an agent and observe the state of the computation (e.g. to show and hide a spinner). We didn’t want to have to wire observation hooks or callbacks directly into the process, so we created a contextual function (defn with-process-callbacks [fire fun & args]). with-process-callbacks returns a function which calls fun with args, but calls fire whenever it’s about to start the process or the process finishes.

In Ruby, calling something like this might entail agent.send { with_callbacks { do_something 1, 2, 3}}. If with_callbacks didn’t follow the above pattern, an invocation would probably look like (send agent (fn [v] (with-process-callbacks callback #(f v 1 2 3)))) in Clojure. By forwarding arguments, however, we can instead write (send agent with-process-callbacks f 1 2 3).