clojure Transducers Creating/Using Transducers


So the most used functions on Clojure map and filter have been modified to return transducers (composable algorithmic transformations), if not called with a collection. That means:

(map inc) returns a transducer and so does (filter odd?)

The advantage: the functions can be composed into a single function by comp , which means traversing the collection just once. Saves run time by over 50% in some scenarios.


(def composed-fn (comp (map inc) (filter odd?)))


;; So instead of doing this:
(->> [1 8 3 10 5]
     (map inc)
    (filter odd?))
;; Output [9 11]

;; We do this: 
(into [] composed-fn [1 8 3 10 5])
;; Output: [9 11]