First of all, if you didn’t read the previous posts, go do so, otherwise you might be missing some essential concepts!
Kotlin Functors, Applicatives, And Monads in Pictures. Part 1/3
Kotlin Functors, Applicatives, And Monads in Pictures. Part 2/3
Monads
How to learn about Monads:
- Get a PhD in computer science.
- 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 <A, B> Option<A>.flatMap(f: (A) -> Option<B>): Option<B> =
when (this) {
is None -> this
is Some -> f(value)
}
Let’s see an example. Good old Option (Maybe
in Haskell) 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:
inline fun <A, B> Option<A>.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
- A functor is a type that implements
map
. - An applicative is a type that implements
apply
. - A monad is a type that implements
flatMap
. Option
implementsmap
andflatMap
, plus we can extend it to implementapply
, 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 coding!
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!
By Alberto Ballano on April 11, 2017.
Exported from Medium on March 3, 2023.