Example 1 — Official Sample
Add 1 to each number and keep the first two, as in the Ramda docs.
const numbers = [1, 2, 3, 4];
const transducer = R.compose(R.map(R.add(1)), R.take(2));
console.log(JSON.stringify(R.into([], transducer, numbers))); // [2,3]
const intoArray = R.into([]);
console.log(JSON.stringify(intoArray(transducer, numbers))); // [2,3] How It Works
- Steps.
R.map(R.add(1))andR.take(2)have no list, soR.composejoins them into a transducer. - Result type.
[]means “collect into a new array”. - One pass.
1 → 2and2 → 3are kept; thentake(2)is full and the loop stops. - Curry.
R.into([])waits for a transducer and a list, sointoArrayis reusable.
