Last active
May 27, 2017 20:13
-
-
Save roblafeve/8f44c2dfb7ee480315f1220d2d8ebca7 to your computer and use it in GitHub Desktop.
Recursive Reduce
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
// 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