Example 1 — Official: Collect Until Greater Than 3
The sample from the Ramda docs: build a list and stop at the first item above 3.
const upToThree = (acc, item) =>
item > 3 ? R.reduced(acc) : acc.concat(item);
console.log(R.reduce(upToThree, [], [1, 2, 3, 4, 5]));
// [1, 2, 3] How It Works
- Normal steps.
1,2, and3are not above 3, soconcatadds each to a new array. - Trigger.
4 > 3is true, so the reducer returnsR.reduced(acc)withacc = [1, 2, 3]. - Stop.
R.reducesees the wrapper, unwraps it, and ends the loop. - Skipped.
4is not added (we wrapped the oldacc), and5is never visited.
