-
Notifications
You must be signed in to change notification settings - Fork 136
ListT
ListT is an AnyM backed implementation of a ListTransformer. That is a type that allows us to manipulate nested Lists directly.
It is used as a return type for groupedT and slidingT operations on cyclops-react Sequential types (e.g. ReactiveSeq, LazyFutureStream, ListX, SetX, PStackX etc). Like AnyM, ListT has two sub-types ListTValue and ListTTSeq
ReactiveSeq<ListX<Integer>> streamOfLists = ReactiveSeq.of(1,2,3,4,5,6)
.grouped(2);
//ReactiveSeq[List[1,2],List[3,4],List[5,6]]
streamOfLists.map(list->list.map(i->i*2));
//ReactiveSeq[List[2,4],List[6,8],List[10,12]]
ListT<Integer> nestedLists = ReactiveSeq.of(1,2,3,4,5,6)
.groupedT(2);
//ListT[ReactiveSeq[List[1,2],List[3,4],List[5,6]]]
nestedLists.map(i->i*2);
//ListT[ReactiveSeq[List[2,4],List[6,8],List[10,12]]]
ListTValue represents an List that is nested inside another monad that resolves to a single value (e.g. an Optional / Maybe / Either / Try / Future type).
ListTSeq represents an Optional that is nested inside another monad that resolves to a sequence of values (e.g. an Stream or List type).
You can build ListT from static creational methods on ListT such as fromValue or fromIterable
ListTValue<Integer> valueT = ListT.fromValue(Maybe.just(Arrays.asList(10));
ListTSeq<Integer> seqT = ListT.fromIterable(Arrays.asList(Arrays.asList(),ListX.of(10));
transformation operation
valueT.map(i->i*2);
//ListT[Maybe[List[20]]
flattening transformation
valueT.flatMap(i->Maybe.just(i*2));
//ListT[Maybe[List[20]]
A flatMap operator where the transforming function returns another monad transformer
valueT.flatMapT(i->MaybeT.fromOptional(Arrays.asList(Maybe.just(i*2)));
//ListT[Maybe[List[20]]
- ListT#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.
- ListTValue#toXXXX, ListTSeq#toXXX there are a large range of conversion operators available on the ListTValue and ListTSeq types that can convert ListT's to JDK or cyclops-react monadic types
- ListT#collect JDK 8 collectors can be used to convert an ListT from one type to another
- ListT#to The to method allows both custom operators and custom converters to be used to convert an ListT to another type
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 a nested sequence type we can write a generic method like so ->
public AnyM<int> sumValues(ListTSeq<Integer> sequence){
return sequence.reduce(Monoids.intSum);
}
In this example we will get access to an AnyM which contains the reduced value from all the nested Lists.
AnyM extends Publisher so other reactive Streams implementations can subscribe to our AnyM type.
ReactiveSeq<Integer> stream = ReactiveSeq.of(1,2,3).map(Arrays::asList);
ListTSeq<Integer> seq = ListT.fromIterable(stream);
Flux<Integer> flux = Flux.from(seq);
Similarly we can provide a consumer to listen to each event generated by the wrapped monadic type as we iterate over it.
ListTValue<Integer> value = ListT.fromValue(FutureW.ofResult(Arrays.asList(10)));
ListTSeq<Integer> seq = ListT.fromIterable(ReactiveSeq.of(Arrays.asList(),
Arrays.asList(10),
Arrays.asList(20),
Arrays.asList(30)));
value.forEach(System.out::println);
//10
seq.forEach(System.out::println);
//10
//20
/30
oops - my bad