Proposal: Array.prototype.accumulate and Array.prototype.accumulateRight

Bergi a.d.bergi at web.de
Thu Oct 27 18:56:49 UTC 2016


Mark M. Young wrote:
> I am proposing a replacement for Array.prototype.reduce and Array.prototype.reduceRight.

Nice. Allowing to pass an argument for the `this` value was missing when 
folding an array, and your suggestion `accumulate` is a fitting method name.

However, I fear the use cases are too unimportant to need a fix.
The `this` keyword is becoming less and less used for anything but class 
methods. And for those, arrow functions solve the problem needing to 
invoke functions on the expected receiver.
For passing in data to a reusable reducer function, closures are much 
simpler than setting the `this` value for the callback.

https://github.com/MarkMYoung/ArrayAccumulate 
<https://github.com/MarkMYoung/ArrayAccumulate> says
> `reduce` is not reusable code because there is no way to know what the second parameter was once beyond the first iteration.

That makes no sense. The whole point of the initial value parameter is 
that it only goes into the first call (if any), or is the return value 
in case the array is empty.

> the second parameter is always available as `this`.

It really should not be. If you want to pass a value for `this`, it 
needs a third parameter.

kind regards,
  Bergi


More information about the es-discuss mailing list