-
Notifications
You must be signed in to change notification settings - Fork 135
Predicates
johnmcclean-aol edited this page Nov 22, 2016
·
3 revisions
Predicates defines a number of predicates for filtering cyclops-react data structures and for pattern matching.
We can use Predicates to filter a Stream
import static com.aol.cyclops.util.function.Predicates.greaterThan;
Stream.of(1,2,3,100,200,300)
.filter(greaterThan(10));
//Stream[100,200,300]
Predicates compose
import static com.aol.cyclops.util.function.Predicates.*;
ReactiveSeq.of(1,2,3).filter(anyOf(not(in(2,3,4)),in(1,10,20)));
ListX.of(1,2,3).filter(anyOf(not(greaterThan(2)),in(1,10,20)));
PStackX.of(1,2,3).filter(anyOf(not(greaterThanOrEquals(2)),in(1,10,20)));
QueueX.of(1,2,3).filter(anyOf(not(lessThan(2)),in(1,10,20)));
ReactiveSeq.of(1,2,3).filter(anyOf(not(lessThanOrEquals(2)),in(1,10,20)));
ReactiveSeq.of(1,2,3).filter(anyOf(not(eq(2)),in(1,10,20)));
Stream.of(Maybe.of(2)).filter(eqv(Eval.now(2)));
Maybe.just(2).filter(eqv(Eval.later(()->2)); //Maybe[2]
We can use Predicates when Pattern Matching
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
import static com.aol.cyclops.util.function.Predicates.*;
Matchables.match2(100,2)
.matches(c->c.is(when(Predicates.greaterThan(50),Predicates.lessThan(10)), ()->"large and small"), ()->"not large and small")
.get();
//"large and small"
Matchables.url(new URL("http://www.aol.com/path?q=hello"))
.on$12_45()
.matches(c->c.is(when(eq("http"),in("www.aol.com","aol.com"),any(),not(eq("q=hello!"))), then("correct")),otherwise("miss"));
//Eval["correct"]
oops - my bad