1.
Data structures with maps and FlatMap seem to be quite common.
In fact there's a name describes this class of a data structures
Together with some algebraic laws that they should has.
They is called monads.
Data structures with map and flatmap are very common
Classes with such data structures plus some algebraic rules are called monads.
2. Flatmap and Unit
A monad M is a parametric type m[t] with the operations, FLATMAP and
Unit, that has to satisfy some laws.
Trait M[t] {def flatmap[u] (f:t = M[u]): M[u]}def unit[t] (x:t): M[t]
3.Examples of Monads
List is a monad with unit (x) = List (x) Set are monad with unit (x) = set (x) Option was a monad with unit (x) = Some (x) Generat Or is a monad with unit (x) = single (x)
FlatMap is a operation on each of the these types, whereas unit in Scala is
Different for each monad
4. Define the map with Flatmap and unit on monads
M map f = = m FlatMap (x = = Unit (f (x)))
5. Monads Laws
Binding law
M flatMap f flatMap g = = M FlatMap (x = f (x) FlatMap g)
Left unit
Unit (x) FlatMap F = = f (x)
Right unit
M FlatMap unit = m
6.monads and for expressions
The binding law makes the following two statements equivalent
For (y <-to (x <-m; y <-f (x)) yield yz <-g (y)) yield z== for (x <-m;y <-f (x) Z <-g (y)) yield Z
Right cell makes
for (x <-m) yield x = = m
7. Try Type
Abstract class Try[+t]case class Success[t] (x:t) extends Try[t]case class Failure (ex:exception) extends try[nothing]
An expression composed from ' Try ', ' map ', ' FlatMap ' 'll Never
Throw a Non-fatal exception.
A statement made up of try map Flatmap never throws a non-fatal exception
This property is called the "bullet prooof" principle.
(not quite understood so far)
REACTIVE programming 1.5 Monods