Normally, arr.map(f).filter(g) creates two intermediate arrays. A transducer fuses map and filter into a single reduction pass — composing transformers that run per-element without intermediate collections. This article builds transducers from scratch.
Prerequisites: FP #1 — compose/pipe
1. The Problem — Intermediate Arrays
Loading editor...
For 10 items, no problem. For 10 million items, 3 intermediate allocations matter.
2. Transducer Building Blocks — Reducing Functions
A transducer is a function that transforms one reducer into another reducer:
Loading editor...
3. Putting It All Together
Loading editor...
4. Visual Comparison
Loading editor...
5. Complete Minimal Transducer Library
Loading editor...
Key Takeaways
- A transducer is
(reducer) → reducer— it transforms how you accumulate. mapping(fn)→ appliesfnbefore passing to the next reducer.filtering(pred)→ skips items that don't pass before passing to next reducer.- Composition of transducers happens at the reducer level — items flow through all transforms in a single pass.
- Transducers are the foundation of Clojure's sequence operations and are available in JS via Ramda and other FP libraries.
Next: FP #3 — Maybe & Either Monads — build error-safe pipelines without null checks or try/catch.
