Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Last active December 21, 2015 01:09
Show Gist options
  • Save ChrisBuchholz/6225698 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/6225698 to your computer and use it in GitHub Desktop.
// 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