Underscore _.reduce clarification?
javascript, underscore.js
Solution
Yes, that's correct. The first argument to the `reduce` callback represents the value returned from the last iteration (or the seed when in the first iteration).
The second argument to the callback is the value of the current iteration of the Array.
As such, the first argument is an accumulator of whatever result you're trying to reach. The final value is returned from the `_.reduce` function when all iterations are complete.
Problem
I just started exploring the JavaScript Underscore library more in-depth and just want to clarify what I think `_.reduce()` (also known as `inject` and `foldl`) does is right. My question is: is the below right? When `_.reduce([1, 2, 3, 4, 5], function(memo, num) { return memo + num; }, 5);` is called, the following happens: - `memo` starts at `5` - `memo` + `list[0]` = `memo` = `6` - `memo` + `list[1]` = `memo` = `8` - `memo` + `list[2]` = `memo` = `11` - `memo` + `list[3]` = `memo` = `15` - `memo` + `list[4]` = `memo` = `20` And that is why the ran function returns `20`. Is that right? And therefore `_.reduceRight()` is the opposite and starts from `memo` + `list[ /* last element in array */ ]`? Thanks. Regards.