short-circuiting Array.prototype.reduce

Kyle Simpson getify at gmail.com
Fri Mar 27 16:29:49 UTC 2015


> I think you could write that like this:
> 
>    outer = outer.filter(arr =>
>      !arr.some((e, i) =>
>        i > 0 && arr[i-1] === e));

Yes, you are of course correct. What I was doing in the originally cited code was illustrating using how `reduce(..)` by its nature supports the adjacency check, instead of using indexes and manual `i-1` type logic.

IOW, I initially wanted to avoid the ugly `i-1`, and I traded that for the unfortunate lack of early exit necessitating the equally ugly `prev === false`. :/





More information about the es-discuss mailing list