Here is a classic example of using higher order functions:
;; a function returns another function (defn make-multiplyer [times] (fn [x] (* x times))) ;; now we bind returned function to a symbol to use it later (def multiply-by-two (make-multiplyer 2)) ;; let use it (multiply-by-two 100) ; => 200
In this code example, fn inside fn works the same. When map invokes (fn [t] (fn [] ...)), it gets the internal fn.
(def list-of-funcs (map (fn [t] (fn [] (* t 10))) ; main part (range 5))) ;; Nearly same as ;; (def list-of-funcs (list (fn [] (* 0 10)) ;; (fn [] (* 1 10)) ;; ... ;; (fn [] (* 4 10)))) (for [i list-of-funcs] (i)) ; => (0 10 20 30 40)
Update. And as Alex said, the tasks in the code sample are tied to a list of calls, which are then passed to .invokeAll ().
koddo
source share