-
Notifications
You must be signed in to change notification settings - Fork 51
cyclops monad api : flatMap on Optional returns a Stream (or flatMap from a single valued monad returning a multi valued monad)
Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react
All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.
AnyM is an abstraction over any monad type, some monads such as CompletableFuture & Optional hold a single value (collections in this case are still treated as a single value) while others such as Stream and List can hold multiple values. In Cyclops the AnyM type follows the type of the underlying monad, that is
Stream<Integer> stream = Stream.of(1,2,3);
corresponds to
AnyM<Integer> anyM = AnyM.fromStream(stream);
Stream<Integer> corresponds to AnyM<Integer>
Similarly
Optional<Integer> optional = Optional.of(1);
corresponds to
AnyM<Integer> anyM = AnyM.fromStream(optional);
Optional<Integer> corresponds to AnyM<Integer>
Returning a Stream in the flatMap method of Optional requires either that we select only a single value from the Stream or change our return type on the flatMap. To see the required signature change
Optional.of(1,2,3).flatMap(i->Stream.of(1,2,3,4))
implies something like
Optional<List<Integer>>
rather than Optional<Integer>
As we can't change the signature, we simply select the first value from the Stream. In this regard we follow the same rules as Javaslang