Skip to content

Instantly share code, notes, and snippets.

@wulymammoth
Last active September 10, 2015 20:22
Show Gist options
  • Select an option

  • Save wulymammoth/df204e38e3b60f31b8d7 to your computer and use it in GitHub Desktop.

Select an option

Save wulymammoth/df204e38e3b60f31b8d7 to your computer and use it in GitHub Desktop.
// using native JS
function filter(list, predicate) {
list.reduce(function(prev, curr, i, arr) {
if (!predicate(curr)) arr.splice(i, 1);
}, []);
return list;
}
// using Underscore
function filter(list, predicate) {
_.reduce(list, function(prev, curr, i, arr) {
if (!predicate(curr)) arr.splice(i, 1);
}, []);
return list;
}
filter([1,2,3,4,5,6], function(sub) { return sub % 2 === 0 }); // [2,4,6]
@wulymammoth
Copy link
Author

Also not a fan of the approach because I'm mutating the original collection. This is not FP (functional programming), but yeah.

In FP, we always create a brand new structure.

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