Skip to content

OptionalT

johnmcclean-aol edited this page Nov 22, 2016 · 3 revisions

cyclops-react : User guide

OptionalT

OptionalT class allow you to manipulate an Optional/s wrapped inside any other monad type (via AnyM). Like AnyM, OptionalT has two sub-types OptionalTValue and OptionalTSeq

OptionalTValue

OptionalTValue represents an Optional that is nested inside another monad that resolves to a single value (e.g. an Optional / Maybe / Either / Try / Future type).

OptionalTSeq

OptionalTSeq represents an Optional that is nested inside another monad that resolves to a sequence of values (e.g. an Stream or List type).

OptionalT

Other monad transformers in Cyclops

Creating an OptionalT

You can build OptionalT from static creational methods on OptionalT such as fromValue or fromIterable

OptionalTValue<Integer> valueT = OptionalT.fromValue(Maybe.just(Optional.of(10));
OptionalTSeq<Integer>  seqT = OptionalT.fromIterable(Arrays.asList(Optional.empty(),Optional.of(10));

Transforming OptionalT

map

transformation operation

valueT.map(i->i*2);

//OptionalT[Maybe[Optional[20]]

flatMap

flattening transformation

valueT.flatMap(i->Maybe.just(i*2));

//OptionalT[Maybe[Optional[20]]

flatMapT

A flatMap operator where the transforming function returns another monad transformer

valueT.flatMapT(i->MaybeT.fromOptional(Optional.of(Maybe.just(i*2)));

//OptionalT[Maybe[Optional[20]]

Converting to other types

  1. OptionalT#unwrap will return the wrapped monad, in general this should only be used locally within the same method that the AnyM is created so we can be sure of it's type.
  2. OptionalTValue#toXXXX, OptionalTSeq#toXXX there are a large range of conversion operators available on the OptionalTValue and OptionalTSeq types that can convert OptionalT's to JDK or cyclops-react monadic types
  3. OptionalT#collect JDK 8 collectors can be used to convert an OptionalT from one type to another
  4. OptionalT#to The to method allows both custom operators and custom converters to be used to convert an OptionalT to another type

Folding / Reduction

AnyM types also have a full range of fold / reduce operators available, which means that data can often be extracted in useful form without conversion back to an unwrapped monadic form.

E.g. to sum all the values in an OptionalT we can write a generic method like so ->

public int sumValues(OptionalTValue<Integer> sequence){
     return sequence.reduce(Monoids.intSum);
}  

Reactive Streams

AnyM extends Publisher so other reactive Streams implementations can subscribe to our AnyM type.

ReactiveSeq<Integer> stream =  ReactiveSeq.of(1,2,3).map(Optional::of);
OptionalTSeq<Integer> seq = OptionalT.fromIterable(stream);

Flux<Integer> flux = Flux.from(seq); 

forEach, forEachWithErrors

Similarly we can provide a consumer to listen to each event generated by the wrapped monadic type as we iterate over it.

OptionalTValue<Integer> value = OptionalT.fromValue(FutureW.ofResult(Optional.of(10)));
OptionalTSeq<Integer> seq = OptionalT.fromIterable(ReactiveSeq.of(Optional.empty(),
                                                                  Optional.of(10),
                                                                  Optional.of(20),
                                                                  Optional.of(30)));

value.forEach(System.out::println);
//10

seq.forEach(System.out::println);
//10
//20
/30
  
Clone this wiki locally