Last active
May 12, 2019 12:14
-
-
Save Slackwise/a88f39e16fd84f6ac43f6b751edd514e to your computer and use it in GitHub Desktop.
Code golf reduce() in JavaScript.
This file contains 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
// Impure | |
reduce = (f, i, l) => (n = [...l]).length ? r(f, f(i, n.pop()), n) : i | |
// Impure and minified | |
r=(f,i,l)=>(n=[...l]).length?r(f,f(i,n.pop()),n):i | |
// Pure | |
reduce = (reducerFunction, initialValue, enumerable) => | |
enumerable.length | |
? reduce(reducerFunction, reducerFunction(initialValue, enumerable[enumerable.length-1]), enumerable.slice(0, -1)) | |
: initialValue; | |
// Pure and minified | |
reduce = (f, i, l) => l.length ? reduce(f, f(i, l[l.length-1]), l.slice(0, -1)) : i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment