(let [xf (comp
(map inc)
(filter even?))]
(transduce xf + [1 2 3 4 5 6 7 8 9 10]))
;; => 30
This example creates a transducer assigned to the local xf
and uses transduce
to apply it to some data. The transducer add's one to each of it's inputs and only returns the even numbers.
transduce
is like reduce
, and collapses the input collection to a single value using the provided +
function.
This reads like the thread-last macro, but separates the input data from the computations.
(->> [1 2 3 4 5 6 7 8 9 10]
(map inc)
(filter even?)
(reduce +))
;; => 30