module String

from Microsoft.FSharp.Core

Monads are not Burritos

Explaining monads to the uninitiated

Marcus Griep
@neoeinstein

A Challenger Appears
Let me show you how to penguin
This is amazing!
>>=
Option.bind
m-bind
do & <-
maybe {} & let!
domonad
Your friend asks "What does that do?"
Your choices:
    1. Oh, that's the Maybe monad!
Your answer: 1
So what is a monad?

Game Over

Your friend asks "What does that do?"
Your choices:
    1. Oh, that's the Maybe monad!
    2. It chains together two functions
       that may or may not produce
       a value.
Your answer: 2
Two functions represented as railway tracks
Two functions composed together as railway tracks

Monad and Applicative in Elm

Monadic bind / >>=

1: 
2: 
3: 
postTime : (Time -> String) -> Task Http.Error String
postTime resourceUrl =
  Time.now `Task.andThen` (resourceUrl >> Http.getString)

Applicative apply / <*>

1: 
2: 
3: 
tryMkPerson : Maybe String -> Maybe Int -> Maybe Person
tryMkPerson name age =
  Person `Maybe.map` name `Maybe.andMap` age
Reader
Chains an argument or environment through a computation to produce a value
State
Mimics mutation by threading a state through a computation to produce a value
IO
Chains together the intent to perform certain effects as part of a computation to produce a value, delaying the effects until they are later executed
Monad is the pattern

So what is a monad?

A monad is a structure that defines how to chain its operations together so that those operations compose sanely.

Three easy steps

  1. Don't start with "Oh! That's a monad!"
  2. Focus on explaining concrete examples
  3. Name the pattern as it emerges