Skip to content

Instantly share code, notes, and snippets.

@Slackwise
Last active May 12, 2019 12:14
Show Gist options
  • Save Slackwise/a88f39e16fd84f6ac43f6b751edd514e to your computer and use it in GitHub Desktop.
Save Slackwise/a88f39e16fd84f6ac43f6b751edd514e to your computer and use it in GitHub Desktop.
Code golf reduce() in JavaScript.
// 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