Last active
September 10, 2015 20:22
-
-
Save wulymammoth/df204e38e3b60f31b8d7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.