-
Notifications
You must be signed in to change notification settings - Fork 134
For
johnmcclean-aol edited this page Nov 22, 2016
·
10 revisions
The For class in cyclops-react provides For comprehensions for Publishers, Values and access to the lower level Do builder for other types (iterable, reader, future, optional, Stream anyM).
## Examples
We can perform a For comprehension with a Filter over a Flux type (from Pivotal's Reactor project) and a cyclops-react ReactiveSeq. This will visit every element of both Streams. In this example we will filter out any combination where the total is less than 10.
import static com.aol.cyclops.control.For.Publishers.each2;
ListX<Tuple2<Integer,Integer>> list;
ListX<Tuple2<Integer,Integer>> list2;
list = each2(Flux.range(1,10),
i-> ReactiveSeq.iterate(i,a->a+1).limit(10),
(a,b)->a+b<10,
Tuple::tuple)
.toListX();
list2 = Flux.range(1,10)
.flatMap(i-> ReactiveSeq.iterate(i,a->a+1)
.limit(10)
.filter(a->i+a<10)
.map(a->Tuple.tuple(i,a)))
.toList()
.block();
assertThat(list,equalTo(list2));With For.Publishers we can execute For comprehensions, that perform nested loops over any Publisher type.
import org.jooq.lambda.tuple.Tuple;
import org.jooq.lambda.tuple.Tuple4;
import static com.aol.cyclops.control.For.Publishers.each4;
ListX<Tuple4<Integer,Integer,Integer,Integer>> list;
list = each4(Flux.range(1,10),
a-> ReactiveSeq.iterate(a,i->i+1).limit(10),
(a,b) -> Maybe.<Integer>of(a+b),
(a,b,c) -> Mono.<Integer>just(a+b+c),
Tuple::tuple)
.toListX();oops - my bad