DEV Community

loading...

Discussion on: The only thing you need is... reduce

Collapse
diasbruno profile image
Bruno Dias • Edited

Last week, on a stack meeting, I explained the same derivations using reduce. :)

It's also nice to point out that you can also implement reduce on custom containers.

class Identity { // Just holds a single value 
  constructor(x) { this.x = x; }
  reduce(f, initial) { return f(initial, this.x); }
}

const base = new Identity(1);
base.reduce((acc, x) => acc + x, 10); // 11
Enter fullscreen mode Exit fullscreen mode