Example 1 — Official Subtract
The sample from the Ramda docs, next to R.reduce with the same arguments.
const nums = [1, 2, 3, 4];
console.log(R.reduceRight(R.subtract, 0, nums)); // -2
console.log(R.reduce(R.subtract, 0, nums)); // -10 How It Works
- Right first.
R.reduceRightstarts with the last item,4, and the seed0. - Value first. Each step is
R.subtract(value, acc):4 - 0 = 4,3 - 4 = -1,2 - (-1) = 3,1 - 3 = -2. - Left fold.
R.reducecallsR.subtract(acc, value)from the front:0 - 1 - 2 - 3 - 4 = -10. - Lesson. For order-sensitive operations like subtraction, direction changes the answer.
