Re:Java 9 features announced
by Paradigma11 on Wednesday August 20, 2014 @07:14AM
(#8201857)
Monad<String> m1 = Optional.lift("hello");
Monad<String> m2 = List.lift("hello");
Shouldn't that be Option<String> m1 = Optional.lift("hello");
List<String> m2 = List.lift("hello");
?
1) Lift regular values into monadic ones in a generic way. Let's say that we have two Monads - Optional and List. There should be a way such that we can take a non-monadic value (the part that will go inside) and turn it into a Monad. So, assuming Monad is a Java-like abstract class, the following should be possible:
2) Flat-map, typically called bind in this context. Given a monad and a function that takes a value and returns a monad, we should have some way of combining the resulting monads if we were to map this function over the first monad's internal value. So for instance, given an optional string, and a function that takes a string and returns an optional int, we should have some way of combining the Optional<Optional<Integer>> into just an Optional<Integer>. So, in the following contrived example... ... you can compare the differences between it and the Functor's map: As it turns out, you can implement the Functor's map method in a generic way using lift and bind.