Before you go, check out these stories!

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

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

Author profile picture

@aballanoAlberto Ballano

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

Monads

How to learn about Monads:

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

Monads add a new twist.

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:

Just a monad hanging out.

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)
// None
Some(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.

Thanks for reading through this article series, if you have any feedback, suggestion, or error to report please tweet me @aballano, or leave a comment below.
If you want to play around with the code head over to GitHub and clone the Playground
Once again, thanks Adit for the wonderful post, and for all the other great ones on the blog.

Happy Koding!

Tags

Become a Hackolyte

Level up your reading game by joining Hacker Noon now!