HardPro challengePythonJavaScriptTypeScript
Transducer
Functional ProgrammingTransducersPerformance
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 transducer
Sample tests
Test #1filter(isEven) then map(double): [2,4] → [4,8]
Input: [[1,2,3,4,5],"filterMap"]
Output: [4,8]
Test #2map(addOne) then filter(isEven): [2,3,4,5,6] → [2,4,6]
Input: [[1,2,3,4,5],"mapFilter"]
Output: [2,4,6]
Test #3sum of evens: 2+4+6 = 12
Input: [[1,2,3,4,5,6],"sum"]
Output: 12