-
Notifications
You must be signed in to change notification settings - Fork 51
Matchable exmaples
johnmcclean-aol edited this page Feb 24, 2016
·
8 revisions
Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react
All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

Matchable.of(Optional.of(3))
.matches(
o-> o.isEmpty().then(i->"hello"),
o-> o.hasValues(1).then(i->""+2),
o-> o.hasValues(2).then(i->""+3),
o-> o.hasValues(3).then(i->""+4)
)
//4
Optional<String> result = Matchable.of(Optional.empty()).mayMatch(c->c.isEmpty().then(i->"hello"))
//Optional["hello"]
Matchable.of(1)
.matches(c->c.isType((Integer it)->"hello").anyValues())
//hello
Matchable.listOfValues(1,2,3)
.matches(c->c.hasValuesWhere((Object i)->(i instanceof Integer)).then(i->2)
//2
Matchable.listOfValues(1,new MyCase(4,5,6))
.matches(c->c.hasValues(Predicates.__,Predicates.hasValues(4,5,6)).then(i->"rec"));
//rec
Matchable.listOfValues(1,new MyCase(4,5,6))
.matches(c->c.hasValues(__,hasValues(4,5,6)).then(i->"rec"));
//rec
Matchable.of(new Child(10,20))
.matches(
c-> c.isType( (Child child) -> child.val).hasValues(10,20)
)
@Value
static class Child extends Parent{
int nextVal;
public Child(int val,int nextVal) { super(val); this.nextVal = nextVal;}
}
@AllArgsConstructor(access=AccessLevel.PACKAGE)
static class Parent{
int val;
}
List<String> result = LazyFutureStream.of(1,2,3,4)
.capture(e->e.printStackTrace())
.patternMatch("",
c->c.hasValuesWhere( (Integer i)->i%2==0 ).then(i->"even"),
c->c.hasValuesWhere( (Integer i)->i%2!=0).then(i->"odd")
)
.toList();
assertThat(result,equalTo(Arrays.asList("odd","even","odd","even")));
List<String> result = LazyFutureStream.of(new MyCase(1,2),new MyCase(3,4))
.capture(e->e.printStackTrace())
.patternMatch("n/a",
c->c.hasValues(1,2).then(i->"one"),
c->c.hasValues(3,4).then(i->"two"),
c->c.hasValues(1,4).then(i->"three"),
c->c.hasValues(2,3).then(i->"four")
)
.toList();
assertThat(result,equalTo(Arrays.asList("one","two")));