Created
May 16, 2016 18:01
-
-
Save MasseGuillaume/24d25b56267574fd604f2432f9ec29ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicativeSection extends Section("applicative") { | |
import cats._ | |
import cats.std.all._ | |
md""" | |
`Applicative` extends `Apply` by adding a single method, `pure`: | |
``` | |
def pure[A](x: A): F[A] | |
``` | |
This method takes any value and returns the value in the context of | |
the functor. For many familiar functors, how to do this is | |
obvious. For `Option`, the `pure` operation wraps the value in | |
`Some`. For `List`, the `pure` operation returns a single element | |
`List`: | |
""" | |
{ | |
Applicative[Option].pure(1) | |
Applicative[List].pure(1) | |
} | |
md""" | |
Like `Functor` and `Apply`, `Applicative` | |
functors also compose naturally with each other. When | |
you compose one `Applicative` with another, the resulting `pure` | |
operation will lift the passed value into one context, and the result | |
into the other context: | |
""" | |
(Applicative[List] compose Applicative[Option]).pure(1) | |
md""" | |
# Applicative Functors & Monads | |
`Applicative` is a generalization of `Monad`, allowing expression | |
of effectful computations in a pure functional way. | |
`Applicative` is generally preferred to `Monad` when the structure of a | |
computation is fixed a priori. That makes it possible to perform certain | |
kinds of static analysis on applicative values. | |
""" | |
Monad[Option].pure(1) | |
Applicative[Option].pure(1) | |
md"Fail" | |
fail("1 + 1") // doesn't fail | |
fail("er") // fail | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment