Skip to content

FutureWT

johnmcclean-aol edited this page Nov 22, 2016 · 1 revision

cyclops-react : User guide

FutureWT

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

FutureWTValue

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

FutureWTSeq

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

FutureWT

Other monad transformers in Cyclops

Creating an FutureWT

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

FutureWTValue<Integer> valueT = FutureWT.fromValue(Maybe.just(FutureW.ofResult(10));
FutureWTSeq<Integer>  seqT = FutureWT.fromIterable(Arrays.asList(FutureW.future(),FutureW.ofResult(10));

Transforming FutureWT

map

transformation operation

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

//FutureWT[Maybe[FutureW[20]]

flatMap

flattening transformation

valueT.flatMap(i->FutureW.ofResult(i*2));

//FutureWT[Maybe[FutureW[20]]

flatMapT

A flatMap operator where the transforming function returns another monad transformer

valueT.flatMapT(i->FutureWT.fromOptional(Optional.of(FutureW.ofResult(i*2)));

//FutureWT[Optional[FutureW[20]]

Converting to other types

  1. FutureWT#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. FutureWTValue#toXXXX, FutureWTSeq#toXXX there are a large range of conversion operators available on the FutureWTValue and FutureWTSeq types that can convert FutureWT's to JDK or cyclops-react monadic types
  3. FutureWT#collect JDK 8 collectors can be used to convert an FutureWT from one type to another
  4. FutureWT#to The to method allows both custom operators and custom converters to be used to convert an FutureWT 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 any Sequence type we can write a generic method like so ->

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

Reactive Streams

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

ReactiveSeq<FutureW<Integer>> stream =  ReactiveSeq.of(1,2,3).map(FutureW::ofResult);
FutureWTSeq<Integer> seq = FutureWT.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.

FutureWTValue<Integer> value = FutureWT.fromValue(FutureW.ofResult(Future.ofResult(10)));
FutureWTSeq<Integer> seq = FutureWT.fromIterable(ReactiveSeq.of(
                                                                  Future.ofResult(10),
                                                                  Future.ofResult(20),
                                                                  Future.ofResult(30)));

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

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