Skip to content

Instantly share code, notes, and snippets.

@andyhd
Created February 1, 2012 11:43
Show Gist options
  • Save andyhd/1716695 to your computer and use it in GitHub Desktop.
Save andyhd/1716695 to your computer and use it in GitHub Desktop.
Javascript map, reduce and filter
function map(f, a) {
return a.length ? [].concat(f(a[0]), map(f, a.slice(1))) : []
}
function filter(f, a) {
return map(function (x) { return f(x) ? x : [] }, a)
}
var even = function (x) { return x % 2 == 0 };
console.log(filter(even, [1, 2, 3, 4]))
@andyhd
Copy link
Author

andyhd commented Feb 1, 2012

Re-implementing map and filter on arrays, just for fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment