Last active
August 14, 2018 08:38
-
-
Save aeinbu/8633def02d46bcb3ff3f49e3c80b865b to your computer and use it in GitHub Desktop.
Combine predicates for filtering
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
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