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_This is a translation of Functors, Applicatives, And Monads In Pictures from Haskell into Kotlin._hackernoon.com
Kotlin Functors, Applicatives, And Monads in Pictures. Part 2/3_This is a translation of Functors, Applicatives, And Monads In Pictures from Haskell into Kotlin._hackernoon.com
How to learn about Monads:
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 -> thisis 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)// 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
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.
map
.apply
.flatMap
.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?
map
.apply
.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!