Flavio Sousa / Jul 06 2023 / Published
transducers 101
Basic usage of transducers vs. just threading macros. Notice the operation sequence bellow. With transducers you operate on each element of the sequence, one element at the time, instead of mapping all elements with the first map predicate, then mapping the result of that with the second map predicate.
{:deps {org.clojure/clojure {:mvn/version "1.10.3"}
;; complient is used for autocompletion
;; add your libs here (and restart the runtime to pick up changes)
compliment/compliment {:mvn/version "0.3.9"}}}
Extensible Data Notation
{:hello (clojure-version)}
0.0s
(def d (range 0 10))
0.0s
(def fn-1 (fn [el]
(let [el (inc el)]
(prn :1 el)
el)))
(def fn-2 (fn [el]
(let [el (* 1000 el)]
(prn :2 el)
el)))
(->>
d
(map fn-1)
(map fn-2))
0.4s
(def xform
(comp (map fn-1) (map fn-2)))
0.0s
(sequence xform d)
0.4s