Last active
August 26, 2015 15:23
-
-
Save allouis/96e2b9dbadcaa0e9004e to your computer and use it in GitHub Desktop.
reduce implemented recursively, no state
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
function compose(f, g) { | |
return function(x) { | |
return f(g(x)); | |
} | |
} | |
function cons(arr) { | |
return function (val) { | |
return arr.concat(val); | |
} | |
} | |
function map(fn, arr) { | |
return reduce(arr, compose(cons([]), fn)); | |
} |
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
function first(arr) { | |
return arr[0]; | |
} | |
function rest(arr) { | |
return Array.prototype.slice.call(arr, 1); | |
} | |
function reduce(value, fn, arr) { | |
return arr.length ? reduce(fn(value, first(arr), fn, rest(arr))) : value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment