Skip to content

Instantly share code, notes, and snippets.

@webbower webbower/mfr.js
Created Aug 19, 2014

Embed
What would you like to do?
Recursive implementations of map, filter, reduce
// 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
You can’t perform that action at this time.