Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Last active February 20, 2018 02:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoDF/cecb3b49b0ac32799fd54e6d3f9e3f96 to your computer and use it in GitHub Desktop.
Save HugoDF/cecb3b49b0ac32799fd54e6d3f9e3f96 to your computer and use it in GitHub Desktop.
ES6 reduce implementation using recursion and destructuring
function reduce([ head, ...tail ], fn, initial) {
if(head === undefined && tail.length === 0) return initial;
if(!initial) {
const [ newHead, ...newTail] = tail;
return reduce(newTail, fn, fn(head, newHead));
}
return tail.length ? reduce(tail, fn, fn(initial, head)) : [ fn(initial, head) ];
}
@tzkmx
Copy link

tzkmx commented Feb 20, 2018

Wow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment