fun with functors & practical m*****s

Functions in Math

 

  1. Total

  2. Deterministic

  3. No Observable Side-Effects

Box

Monads

In programming; a Monad is a Design Pattern. It’s a structure, a wrapper which “enriches” a value by giving it a context.

Famous examples of Monads are:

  • Option/Maybe monad (it can represent a missing/null value)
  • Either monad (it can represent a successful operation or a failure)
  • IO/Effect monad (it can represent side-effects)
  • Task monad (it can represent asynchronous side-effects)

 

A Monad has 2 particular operations:

  • a unit (often called return or pure) which takes a value and puts it into the container (yielding a so called “monadic value”)
  • a chain (often called bind or flatMap) which concatenates Monads.

For example, when flatMap is introduced in JavaScript Arrays, we will be able to think about it as a Monad:

  • Array.of(1) // Puts 1 into the array
  • Array.map(n => n + 1) // Maps the values into the array, this makes it a Functor
  • Array.flatMap(n => [n,n]) // Flattens the generated arrays into one array, this makes it a Monad
     

For these reasons, a Monad is often referred to as “a Functor which can flatten”.

Made with Slides.com