Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Last active August 14, 2018 08:38
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 aeinbu/8633def02d46bcb3ff3f49e3c80b865b to your computer and use it in GitHub Desktop.
Save aeinbu/8633def02d46bcb3ff3f49e3c80b865b to your computer and use it in GitHub Desktop.
Combine predicates for filtering
const combinePredicates = (predicate1, predicate2, ...predicateRest) =>
(item) =>
predicate1(item) && (predicateRest.length > 0? combinePredicates(predicate2, predicateRest[0]): predicate2)(item);
// sample predicates
const exclude = (val) => (item) => item !== val;
const excludeRange = (from, to) => (item) => item < from || item > to;
// sample data
let arr = [1,2,3,4,5,6,7,8,9,10];
console.log(arr.filter(combinePredicates(exclude(1), exclude(3), excludeRange(5, 9))));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment