Before you go, check out these stories!

0
Kotlin Functors, Applicatives, And Monads in Pictures. Part 3/3 by@aballano

Kotlin Functors, Applicatives, And Monads in Pictures. Part 3/3

@aballanoAlberto Ballano

First of all, if you didn’t read the previous posts, go do so, otherwise you might be missing some essential concepts!

1. Get a PhD in computer science.
2. Throw it away because you don’t need it for this section!

Functors apply a function to a wrapped value:

Applicatives apply a wrapped function to a wrapped value:

Monads apply a function that returns a wrapped value to a wrapped value. Monads have a function `flatMap` (`liftM` or `>>=` in Haskell) to do this.

`inline fun <B> flatMap(f: (A) -> Option<B>): Option<B> = when (this) {    is None -> this    is Some -> f(value)}`

Let’s see an example. Good ol’ Option is a monad:

Suppose `half` is a function that only works on even numbers:

`fun half(a: Int) = when {    a % 2 == 0 -> Some(a / 2)    else -> None}`

What if we feed it a wrapped value?

We need to use `flatMap` (`>>=` in Haskell) to shove our wrapped value into the function. Here’s a photo of `flatMap`:

Here’s how it works:

`Some(3).flatMap(::half)// NoneSome(4).flatMap(::half)// Some(2)None.flatMap(::half)// None`

What’s happening inside? Let’s look at `flatMap`'s (`>>=` in Haskell) signature again:

``// For Option`inline fun <B> flatMap(f: (A) -> Option<B>): Option<B>`

So `Option` is a Monad. Here it is in action with a `Some(3)`!

And if you pass in a `None` it’s even simpler:

You can also chain these calls (by using an infix version of `flatMap`):

`Some(20) flatMap ::half flatMap ::half flatMap ::half// => None`
Note: the original article now describes Haskell’s `IO` Monad. Kotlin doesn't have anything like that so this translation skips it.

Conclusion

1. A functor is a type that implements `map`.
2. An applicative is a type that implements `apply`.
3. A monad is a type that implements `flatMap`.
4. `Option` implements `map` and `flatMap`, plus we can extend it to implement `apply`, so it is a functor, an applicative, and a monad.

What is the difference between the three?

• functors: you apply a function to a wrapped value using `map`.
• applicatives: you apply a wrapped function to a wrapped value using `apply`.
• monads: you apply a function that returns a wrapped value, to a wrapped value using `flatMap`.

So, dear friend (I think we are friends by this point), I think we both agree that monads are easy and a SMART IDEA(tm). Now that you’ve wet your whistle on this guide, why not pull a Mel Gibson and grab the whole bottle. Check out LYAH’s section on Monads. There’s a lot of things I’ve glossed over because Miran does a great job going in-depth with this stuff.