Skip to content

Instantly share code, notes, and snippets.

@ronapelbaum
Created April 1, 2020 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronapelbaum/f2e9c2d9f60c6c8df89f608084d7bc26 to your computer and use it in GitHub Desktop.
Save ronapelbaum/f2e9c2d9f60c6c8df89f608084d7bc26 to your computer and use it in GitHub Desktop.
High Order Functions - Array.prototype.filter
/**
* https://jsfiddle.net/ronapelbaum/qk6mcrae/
*/
// HOF get a func and returns a func
function createFilterFunc(fn) {
return function(arr) {
const res = [];
for(let i = 0; i < arr.length; i++) {
if(!!fn(arr[i])) {
res.push(arr[i]);
}
}
return res;
}
}
// ====================
const arr = [1, 3, 5, 4, 8];
const isEven = d => d%2 === 0;
const filterEven = createFilterFunc(isEven);
console.log('even array.filter', arr.filter(isEven));
console.log('even filterEven', filterEven(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment