Skip to content

For operators

johnmcclean-aol edited this page Dec 6, 2016 · 1 revision

forEach operators

cyclops-react types (Eval, Maybe, [FutureW](FutureW], Xor, Ior, Try, FeatureToggle, Either1-5, ListX, DequeX, SetX, SortedSetX, PStackX, PVectorX, POrderedSetX, PQueueX, PBagX, AnyMValue, AnyMSeq, LazyFutureStream, ReactiveSeq) all have in built native for comprehension operators forEach2-4.

For comprehensions simplify nested flatMap operations. forEach operators accept 2-4 additional functions (for forEach2-4) which are then applied in nested fashion via flatMap and map.

E.g. this nested set of operations on a Maybe could be rewritten

Maybe.just(2)
     .flatMap(n->loadById(n).map(r->n*r))

//if loadById returns 3 we get  Maybe[6]

as

Maybe.just(2)
     .forEach2(this::loadById,(n,r)->n*r)

Resources

Example

Add all combinations of numbers in two lists together

ListX.of(1, 2, 3)
     .forEach2(a -> Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), 
              (a , b) -> a + b);

//List[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 3, 4, 5, 6, 7, 8,9, 10, 11, 12]
Clone this wiki locally