Transducers – a portmanteau of “transform reducers” – are a functional programming concept introduced into the Clojure programming language. Although transducers are actually pretty straightforward in retrospect, wrapping your brain around them, especially if you're not already a competent Clojureist, can be challenging.
We introduce transducers by implementing them from scratch in Python.
We’ll start with the familiar staples of functional programming,
map()
, filter()
and reduce()
, and
derive transducers from first principles. We’ll work towards a set of
general tools which work with eager collections, lazy “pull” sequences, and
“push” event streams. Along the way we’ll cover stateful transducers and
transducer composition, demonstrating that transducers are both more
general, and more fundamental, than the functional programming tools baked
into Python and many other languages.
On completion, not only should transducers make sense to you, but you’ll have a recipe for implementing transducers in your own favourite programming language.