short-circuiting Array.prototype.reduce

Jason Orendorff jason.orendorff at gmail.com
Fri Mar 27 16:23:51 UTC 2015


On Thu, Mar 26, 2015 at 8:22 PM, Kyle Simpson <getify at gmail.com> wrote:
> outer = outer.filter(function filterer(inner){
>   return inner.reduce(function reducer(prev,current){
>     if (prev === false || prev === current) return false;
>     return current;
>   });
> });

I think you could write that like this:

    outer = outer.filter(arr =>
      !arr.some((e, i) =>
        i > 0 && arr[i-1] === e));

Array.prototype.some is designed to produce a boolean result and
already has early exit built in.

-j


More information about the es-discuss mailing list