Skip to content

Instantly share code, notes, and snippets.

@Chadtech
Last active January 11, 2020 06:58
Show Gist options
  • Save Chadtech/0eacf00e114f3db9b096a4f87a9e404f to your computer and use it in GitHub Desktop.
Save Chadtech/0eacf00e114f3db9b096a4f87a9e404f to your computer and use it in GitHub Desktop.
M a ---------- M b
| \ / |
| \ / |
| \ / |
| \/ |
| /\ |
| / \ |
| / \ |
| / \ |
a -------------- b
From each corner to each corner, is a fundamental "monadic" operation
a -> b Simply a function, and applying a function.
Examples: f(x) in many languages, but also `f $ x` in Haskell and
`f <| x` in Elm
a -> M a Taking a value of type `a`, like a `String`, and wrapping it into some
kind of type that can contain other types `M`, like an `Array` or a `Maybe`
Examples: `lift` in Haskell, or just dumping a `x` into `Some()` like you
might in Rust or Scala
M a -> a Trying to get that value out of a monad, usually with a default value in
case you cant.
Examples: `unwrap_or` in Rust, `gerOrElse` in Scala, `withDefault` in Elm
(a -> b) -> M a -> M b Mapping. Taking something that wraps a type `M a`, and then changing the
wrapped type `a` to a new type `b`, using a function `a -> b` without
changing the type that is doing the wrapping `M`. Like if you change a list
of numbers into a list of strings, or a maybe number into a maybe string.
Its just called `map` in most languages, but called `fmap` and `<$>` in
Haskell.
(a -> M b) -> M a -> M b Arguably the most important monady thing. Take `M a`, reach inside `M` to,
get `a`, and using `a` produce the next step in your code, `M b`.
Examples: Promises in JS, where you do something that might fail, but can
assume the success case, work from there and move onto the next step, which
also might fail. Haskell has `bind`, `>>=`, and do notation that exemplify
this concept. Elm uses functions called `andThen`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment