17 Transducers
Lesson goal Understand why a map and filter chain creates intermediate arrays, learn to write transducers as reducer transformers, compose them with compose, use R.transduce and R.into, and judge when it pays off.
A map and filter chain reads well, but it has a cost. Each step walks the whole array and creates a new one. filter(isEven), map(square), filter(isBig) means three passes and three new arrays, two of them intermediate: they are needed only as input for the next step. In the "Performance" lesson we already counted this overhead in callback calls; now let's count the arrays too.
// counters: how many arrays were created and how many times the steps were called
const stats = { arrays: 0, calls: 0 }
// map :: (a -> b) -> [a] -> [b]
const map = (fn) => (arr) => {
stats.arrays += 1
return arr.map((x) => {
stats.calls += 1
return fn(x)
})
}
// filter :: (a -> Boolean) -> [a] -> [a]
const filter = (pred) => (arr) => {
stats.arrays += 1
return arr.filter((x) => {
stats.calls += 1
return pred(x)
})
}
// isEven :: Number -> Boolean
const isEven = (n) => n % 2 === 0
// square :: Number -> Number
const square = (n) => n * n
// isBig :: Number -> Boolean
const isBig = (n) => n > 100000
// the numbers from 1 to 1000
const numbers = R.range(1, 1001)
const result = R.pipe(filter(isEven), map(square), filter(isBig))(numbers)
console.log('numbers in the result: ' + result.length) // numbers in the result: 342
// 1000 calls of the first filter, 500 calls of map and 500 calls of the second filter
console.log('arrays: ' + stats.arrays + ', calls: ' + stats.calls) // arrays: 3, calls: 2000You could merge all the steps into a single reduce, as in the exercise of the "Performance" lesson, but then they would fuse into one callback and stop being separate functions. Transducers give you both: the steps stay separate and composable, and the data goes through them in a single pass, with no intermediate arrays. The idea comes from Clojure, where Rich Hickey proposed transducers in 2014.
A reducer and a reducer transformer
A reducer is the function that reduce takes: (acc, x) => newAcc. It receives the accumulated value and the next element and returns the new accumulated value. Summing is a reducer, and so is appending an element to an array.
A transducer is a reducer transformer: a function that takes a reducer and returns a new reducer. mapping(f) turns a reducer into one that applies f to the element first, and filtering(pred) turns it into one that lets only matching elements through to the original reducer. Neither of them knows what the reducer does: collects an array, computes a sum or builds a string.
// === reducers: (acc, x) -> acc ===
// pushTo :: ([a], a) -> [a]
// changes only the accumulator we created for this fold;
// copying [...acc, x] on every step would mean quadratic work
const pushTo = (acc, x) => {
acc.push(x)
return acc
}
// sum :: (Number, Number) -> Number
const sum = (acc, x) => acc + x
// === transducers: take a reducer and return a reducer ===
// mapping :: (a -> b) -> ((acc, b) -> acc) -> ((acc, a) -> acc)
const mapping = (fn) => (reducer) => (acc, x) => reducer(acc, fn(x))
// filtering :: (a -> Boolean) -> ((acc, a) -> acc) -> ((acc, a) -> acc)
const filtering = (pred) => (reducer) => (acc, x) => (pred(x) ? reducer(acc, x) : acc)
// isEven :: Number -> Boolean
const isEven = (n) => n % 2 === 0
// square :: Number -> Number
const square = (n) => n * n
// by hand: mapping wraps pushTo, filtering wraps the result
const evenSquaresToArray = filtering(isEven)(mapping(square)(pushTo))
console.log([1, 2, 3, 4, 5, 6].reduce(evenSquaresToArray, []).join(', ')) // 4, 16, 36
// the same steps with another reducer: a sum instead of an array
const evenSquaresSum = filtering(isEven)(mapping(square)(sum))
console.log([1, 2, 3, 4, 5, 6].reduce(evenSquaresSum, 0)) // 56Composing transducers
A transducer is a function of one argument, so transducers compose with compose. The order of writing is unusual here: in compose(filtering(isEven), mapping(square)) the elements are filtered first and only then squared. The steps run left to right, as in pipe.
Here is why. compose does apply functions right to left, but it applies them to the reducer, not to the data. First mapping(square) wraps the final reducer, then filtering(isEven) wraps the result. The leftmost wrapper ends up outermost, and every element reaches it first. The wrappers are built right to left, and the data goes through them left to right.
// compose :: ((y -> z), ..., (a -> b)) -> a -> z
const compose = (...fns) => (x) => fns.reduceRight((acc, fn) => fn(acc), x)
// transduce :: (transducer, reducer, acc, [a]) -> acc
const transduce = (xform, reducer, init, arr) => arr.reduce(xform(reducer), init)
// mapping and filtering from the previous example
const mapping = (fn) => (reducer) => (acc, x) => reducer(acc, fn(x))
const filtering = (pred) => (reducer) => (acc, x) => (pred(x) ? reducer(acc, x) : acc)
// counters: arrays are created only with newArray, counted counts the steps
const stats = { arrays: 0, calls: 0 }
const newArray = () => {
stats.arrays += 1
return []
}
const counted = (fn) => (x) => {
stats.calls += 1
return fn(x)
}
const pushTo = (acc, x) => {
acc.push(x)
return acc
}
const isEven = (n) => n % 2 === 0
const square = (n) => n * n
const isBig = (n) => n > 100000
// the steps are written in the order they run: filter, map, filter
const xform = compose(
filtering(counted(isEven)),
mapping(counted(square)),
filtering(counted(isBig)),
)
const result = transduce(xform, pushTo, newArray(), R.range(1, 1001))
console.log('numbers in the result: ' + result.length) // numbers in the result: 342
// one pass, one array (the accumulator), and as many step calls as in the chain
console.log('arrays: ' + stats.arrays + ', calls: ' + stats.calls) // arrays: 1, calls: 2000Transducers in Ramda
In Ramda, map, filter, take and a number of other list functions work as transducers if you call them without data and pass the result to R.transduce or R.into. R.transduce(xform, reducer, init, list) is a fold like our transduce: you supply the reducer and the initial value. R.into(acc, xform, list) picks the reducer from its first argument: elements are appended to an array, concatenated to a string, or have their properties or [key, value] pairs copied into an object. Note that in Ramda 0.32 only the type of the first argument is used, and its contents do not make it into the result: R.into([1, 2], R.map(R.inc), [3]) returns [4], not [1, 2, 4]. So pass an empty value: [], '' or {}. The Ramda docs call an object with the @@transducer/init, @@transducer/step and @@transducer/result methods a transformer, and a function that takes such a transformer and returns a new one a transducer. The idea is the same, just with an object instead of a reducer function.
// isEven :: Number -> Boolean
const isEven = (n) => n % 2 === 0
// the numbers from 1 to 10
const numbers = R.range(1, 11)
// R.filter and R.map without data are transducers
const xform = R.compose(R.filter(isEven), R.map((n) => n * n))
// R.into: the result has the same shape as the first argument
console.log(R.into([], xform, numbers).join(', ')) // 4, 16, 36, 64, 100
console.log(R.into('', R.compose(xform, R.map((n) => n + ';')), numbers)) // 4;16;36;64;100;
// R.transduce: your own reducer and initial value
console.log(R.transduce(xform, R.add, 0, numbers)) // 220
// === one composition, two orders ===
const incThenKeepEven = R.compose(R.map(R.inc), R.filter(isEven))
// as a plain function on an array: compose goes right to left, filter first, then map
console.log(incThenKeepEven([1, 2, 3, 4]).join(', ')) // 3, 5
// as a transducer: left to right, map first, then filter
console.log(R.into([], incThenKeepEven, [1, 2, 3, 4]).join(', ')) // 2, 4
// === stopping early ===
let chainCalls = 0
const chainResult = R.range(1, 1001)
.map((n) => {
chainCalls += 1
return n * n
})
.filter((n) => n > 100)
.slice(0, 3)
console.log(chainResult.join(', ') + ', map calls: ' + chainCalls) // 121, 144, 169, map calls: 1000
let xformCalls = 0
const firstThree = R.compose(
R.map((n) => {
xformCalls += 1
return n * n
}),
R.filter((n) => n > 100),
R.take(3), // three elements taken: the fold stops
)
console.log(R.into([], firstThree, R.range(1, 1001)).join(', ') + ', map calls: ' + xformCalls) // 121, 144, 169, map calls: 13When it pays off
On a full pass transducers don't reduce the number of step calls: every element goes through every step it reaches, just as in the chain. The gain is elsewhere. There are no intermediate arrays and no extra passes. The fold can stop early, as with R.take. The same transformation works with any reducer and any source Ramda's fold understands: an array, an iterable such as a Set or a generator, or an object with a reduce method.
This shows on large collections, long chains and early stopping. For an array of a hundred elements and two steps, a map and filter chain is easier to read and fast enough. ES2025 gave iterators their own lazy map, filter and take methods: they also handle elements one by one, with no intermediate arrays. Transducers differ in that they separate the transformation both from the data source and from the way the result is collected. As in the "Performance" lesson, check the gain by measuring: count arrays and calls, or repeat time measurements, rather than relying on a single run.
Key points
- Each step of a map and filter chain walks the whole array and creates a new one; the intermediate arrays are needed only as input for the next step.
- A reducer is a function (acc, x) => acc, and a transducer is a reducer transformer: it takes a reducer and returns a new one without knowing what that reducer collects.
- compose applies transducers to the reducer right to left, so the leftmost wrapper ends up outermost and the data goes through the steps left to right.
- In Ramda, map, filter and take without data work as transducers: R.transduce folds with your reducer, and R.into collects the result into an array, a string or an object.
- Transducers remove intermediate arrays and extra passes and can stop early; on small arrays and short chains a plain chain is simpler.
Exercises
Exercise 1
Write the mapping and filtering transducers and use compose to build the paidTotals transducer from them: keep the paid orders, take their total, keep the totals of 1000 and more. Apply it to the two reducers from the starter: with pushTo you get an array of totals, with sum their grand total. Think about the order in which to write the steps in compose.
Starter code
const orders = [
{ id: 1, total: 1200, paid: true },
{ id: 2, total: 300, paid: false },
{ id: 3, total: 4500, paid: true },
{ id: 4, total: 800, paid: true },
{ id: 5, total: 2500, paid: false },
]
// compose :: ((y -> z), ..., (a -> b)) -> a -> z
const compose = (...fns) => (x) => fns.reduceRight((acc, fn) => fn(acc), x)
// reducers
// pushTo :: ([a], a) -> [a]
const pushTo = (acc, x) => {
acc.push(x)
return acc
}
// sum :: (Number, Number) -> Number
const sum = (acc, x) => acc + x
// your code: mapping, filtering and paidTotals
console.log(orders.reduce(paidTotals(pushTo), []).join(', '))
console.log(orders.reduce(paidTotals(sum), 0))Expected output
1200, 4500 5700
Show solution
const orders = [
{ id: 1, total: 1200, paid: true },
{ id: 2, total: 300, paid: false },
{ id: 3, total: 4500, paid: true },
{ id: 4, total: 800, paid: true },
{ id: 5, total: 2500, paid: false },
]
// compose :: ((y -> z), ..., (a -> b)) -> a -> z
const compose = (...fns) => (x) => fns.reduceRight((acc, fn) => fn(acc), x)
// reducers
// pushTo :: ([a], a) -> [a]
const pushTo = (acc, x) => {
acc.push(x)
return acc
}
// sum :: (Number, Number) -> Number
const sum = (acc, x) => acc + x
// mapping :: (a -> b) -> ((acc, b) -> acc) -> ((acc, a) -> acc)
const mapping = (fn) => (reducer) => (acc, x) => reducer(acc, fn(x))
// filtering :: (a -> Boolean) -> ((acc, a) -> acc) -> ((acc, a) -> acc)
const filtering = (pred) => (reducer) => (acc, x) => (pred(x) ? reducer(acc, x) : acc)
// the steps are written in the order they run: the leftmost wrapper is outermost,
// so an order is checked for payment first and then turned into its total
const paidTotals = compose(
filtering((order) => order.paid),
mapping((order) => order.total),
filtering((total) => total >= 1000),
)
console.log(orders.reduce(paidTotals(pushTo), []).join(', '))
console.log(orders.reduce(paidTotals(sum), 0))Exercise 2
In the starter, the names of the first three active users are found with a chain of filter, map and slice over a list of 1000 users, and a counter shows how many times the predicate was called. Rewrite the search with R.into, R.compose, R.filter, R.map and R.take using the isActive predicate, and compare the number of calls.
Starter code
// 1000 users, every seventh one is active
const users = R.range(1, 1001).map((id) => ({ id, name: 'user' + id, active: id % 7 === 0 }))
let chainCalls = 0
const isActiveChain = (user) => {
chainCalls += 1
return user.active
}
const chainResult = users.filter(isActiveChain).map((user) => user.name).slice(0, 3)
console.log('chain: ' + chainResult.join(', ') + ', calls: ' + chainCalls)
let xformCalls = 0
const isActive = (user) => {
xformCalls += 1
return user.active
}
// your code: xformResult with R.into
const xformResult = []
console.log('transducer: ' + xformResult.join(', ') + ', calls: ' + xformCalls)Expected output
chain: user7, user14, user21, calls: 1000 transducer: user7, user14, user21, calls: 21
Show solution
// 1000 users, every seventh one is active
const users = R.range(1, 1001).map((id) => ({ id, name: 'user' + id, active: id % 7 === 0 }))
let chainCalls = 0
const isActiveChain = (user) => {
chainCalls += 1
return user.active
}
const chainResult = users.filter(isActiveChain).map((user) => user.name).slice(0, 3)
console.log('chain: ' + chainResult.join(', ') + ', calls: ' + chainCalls)
let xformCalls = 0
const isActive = (user) => {
xformCalls += 1
return user.active
}
// the steps in the order they run: select, take the name, stop at the third one
const firstActiveNames = R.compose(R.filter(isActive), R.map(R.prop('name')), R.take(3))
const xformResult = R.into([], firstActiveNames, users)
// the predicate checked users 1 to 21: at user21 R.take had collected three names
console.log('transducer: ' + xformResult.join(', ') + ', calls: ' + xformCalls)Playground
Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.