A transducer is a composable, efficient transformation that decouples logic from data structure.
Instead of chaining .filter().map() (creates intermediate arrays), a transducer fuses the steps.
// Naive — creates two intermediate arrays
const result = [1, 2, 3, 4, 5]
.filter(isEven) // [2, 4]
.map(double); // [4, 8]
// Transducer — single pass, no intermediate arrays
const xf = compose(filterT(isEven), mapT(double));
transduce(xf, append, [], [1, 2, 3, 4, 5]); // [4, 8]Implement:
mapT(fn) — a mapping transducerfilterT(pred) — a filtering transducertransduce(xf, reducer, init, coll) — runs the transducerSample tests