Last active
February 20, 2018 02:11
-
-
Save HugoDF/cecb3b49b0ac32799fd54e6d3f9e3f96 to your computer and use it in GitHub Desktop.
ES6 reduce implementation using recursion and destructuring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow.