Last active
December 21, 2015 01:09
-
-
Save ChrisBuchholz/6225698 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
| // data | |
| var nums = [1, 2, 3, 4, 5, 6, 7, 8]; | |
| // What you would do if you did not have a functional mindset | |
| var evens = []; | |
| for (var i = 0; i < nums.length; ++i) { | |
| if (nums[i] % 2 === 0) { | |
| evens.push(nums[i]); | |
| } | |
| } | |
| console.log(evens); | |
| // What you would do if you had a functional mindset | |
| var odd = function(x) { return x % 2 === 0 ? true : false; }; | |
| var filter = function(coll, idd) { | |
| var res = []; | |
| for (var i = 0; i < coll.length; ++i) { | |
| if (idd(coll[i])) res.push(coll[i]); | |
| } | |
| return res; | |
| }; | |
| var evens = filter(nums, odd); | |
| console.log(evens); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment