Example 1 — Official Odd Sum
Both samples from the Ramda docs: stop at the first even number.
const isOdd = (acc, x) => x % 2 !== 0;
console.log(R.reduceWhile(isOdd, R.add, 0, [1, 3, 5, 60, 777, 800])); // 9
console.log(R.reduceWhile(isOdd, R.add, 111, [2, 4, 6])); // 111 How It Works
- Check first.
isOddignoresaccand tests only the next number. - First list.
1,3,5pass and are added withR.add:0 + 1 + 3 + 5 = 9. - Stop.
60is even, so the fold returns9without looking at777or800. - Second list.
2fails immediately, so nothing is added and the seed111comes back.
