Created
August 19, 2014 23:31
-
-
Save webbower/ad63384c2e53db813b9e to your computer and use it in GitHub Desktop.
Recursive implementations of map, filter, reduce
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
// Requires utils.list.js for first() and rest() | |
function map(callback, list) { | |
if (!list.length) return []; | |
else return [callback(first(list))].concat(map(callback, rest(list))); | |
} | |
function filter(predicate, list) { | |
if (!list.length) return []; | |
else return (predicate(first(list)) ? [first(list)] : []).concat(filter(predicate, rest(list))); | |
} | |
function reduce(callback, value, list) { | |
if (!list.length) return value; | |
else return reduce(callback, callback(value, first(list)), rest(list)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment