REACTIVE programming 1.5 Monods

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.