Skip to content

Instantly share code, notes, and snippets.

@roblafeve
Last active May 27, 2017 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roblafeve/8f44c2dfb7ee480315f1220d2d8ebca7 to your computer and use it in GitHub Desktop.
Save roblafeve/8f44c2dfb7ee480315f1220d2d8ebca7 to your computer and use it in GitHub Desktop.
Recursive Reduce
// Recursive Reduce
const reduce = (x, y, z) => {
const head = z[0]
return !head
? y
: reduce(x, x(y, head), z.slice(1))
}
// Direct usage of reduce
reduce((acc, x) => acc + x, 0, [1, 2, 3]) // 6
// Create map using reduce
const map = (morph, arr) =>
reduce((acc, x) => acc.concat(morph(x)), [], arr)
map(x => x * 2, [1, 2, 3]) // [2, 4, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment