Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Just Monad" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle creates a Just monad and a mapping of the monad. Then, Kyle walks through calling map and ap functions on the two created monads, explaining what is occurring at each step.

Preview
Close

Transcript from the "Just Monad" Lesson

[00:00:00]
>> Kyle: So here let's make, Just (41). We've now made ourselves a monad called fortyOne. Line 2, let's make a fortyTwo monad that is the mapping from the fortyOne monad. fortyTwo here is not the number 42, it's a monad wrapped around the number 42. Why? Because .map always has to return the same kind of data structure as it started with.

[00:00:27]
Call it .map on an array, get an array, call it on a monad, you're gonna get another monad. All right, well that's fairly straightforward. That's a way of getting one value changed to another. It definitely seems like a lot of pomp and circumstance. [LAUGH] When we're used to just being able to do something like plus 1, this seems like a lot of extra work to be going to.

[00:00:47]
And the only reason you go to this extra work is because these monads have this nice clean way of working together that is more predictable than just regular old values. They have these mathematical principles that we're trying to opt into. That's why you go to the trouble of using a monad.

[00:01:09]
Okay, let's then talk. Remember, we talked early in the course of the identity function, here we're got the identity function again. I just want to show you that if you call chain with a method that only returns a value like identity, then you're literally gonna get the value out.

[00:01:26]
If I say fortyOne.chain(identity), I'm literally gonna get the value 41 out, same thing, line 12 with the value 42. That technically lines 10 through 12 [LAUGH] that technically is a violation of the monadic laws. You're not supposed to call chain with a map or function that won't return a monad.

[00:01:45]
And some monad implementations would actually catch this problem and either throw an error or force it to be wrapped in a monad. My very loose implementation, and I'm only doing this here for our debug inspection purposes. I'm only showing you that you can look at a monad and see what's in it if you sort of violate that monadic law.

[00:02:06]
But that's not what you're supposed to do. What you're supposed to do is once it's in a monad, just make another monad out of it, and make another monad out of that, and so on and so forth. You're never really supposed to extract it out of the monad like that.

[00:02:18]
Okay, so here we are, making monads. We're able to convert one monad to another using a .map. Let's talk about .chain and .ap calls for just a moment. If I made a couple of monads that were wrapped around the strings, Kyle and Susan. And then I had a tuple function.

[00:02:37]
And we've looked at this earlier in the course, but this particular version of tuple which is curry, is going to take in two values and return us an array with those two values. That's what it's job is, is to take in two individual values and make an array with them.

[00:02:52]
By the way, what shape is that? What shape is tuple?
>> Speaker 2: [INAUDIBLE]
>> Kyle: What shape have we been talking about over and over and over again when we were talking about all those transducer discussions? What shape is tuple? It looks like a reducer, right? It's a thing that takes two values and makes one value out of it.

[00:03:18]
In this case it takes two discrete values and makes it a two element array out of it. Get used to thinking about every time you see a function, ask yourself, what kind of shape is it? And if I know what kind of shape it is, what kind of usage can I get out of it?

[00:03:36]
>> Kyle: Okay, so let's look at the .ap call, cuz remember I said the .ap is sort of a strange little beast. The .ap call on this line eight is being called on the result of the .map call. So we're gonna take user1 and map it with tuple. What does user1.map with tuple give us?

[00:04:03]
>> Speaker 3: It connects it [INAUDIBLE]
>> Kyle: Think carefully, if I just did user1.map and I passed in tuple what would we get back?
>> Speaker 4: Function with the x already-
>> Kyle: What is the return value of every .map call?
>> Speaker 4: An array.
>> Kyle: No, it is the data structure type that it was called upon.

[00:04:25]
If we called array.map we're gonna get an array, but here we are calling what?
>> Speaker 4: Monad.
>> Kyle: We're calling monad.map. So what are we gonna get?
>> Speaker 3: A monad
>> Kyle: We're gonna get another monad back. And what is that monad gonna have in it? Well it called tuple with the single value that was inside of user one, which is a string file.

[00:04:44]
Tuple expects two inputs, and it only got called with one input. So what is going to come back to be stuck inside of that intermediary monad?
>> Speaker 2: Curry.
>> Kyle: A single function that is waiting for one more value. So the monad that we have made with the user1.map is a monad that is wrapped around the function that is holding the string Kyle in its closure, okay?

[00:05:15]
And then we're gonna call .ap on that thing. And if you look back in your slides to the previous slide, what does .ap do? It's gonna call user2.map with the function that's in our temporary monad. So we have one value coming out of user1 into the closure, and then we call .ap on another monad and it provides the second value, and what do we get back from that .ap call?

[00:05:45]
That .ap call is under the covers a .map call. So what are we gonna get back?
>> Speaker 3: A monad.
>> Kyle: We're gonna get a monad, thank you. And what is that monad gonna have in it?
>> Speaker 4: A tuple.
>> Kyle: It's gonna have the tuple, thank you very much.

[00:06:01]
If we called users.chain(identity), our little cheating the monadic laws thing, you're gonna see the tuple, Kyle and Susan.
>> Kyle: So .ap is a way for us to take one value out of a monad, put it into a closure, and then use that as the mapper for the next monad.

[00:06:26]
>> Kyle: These three functions, map, chain, or flat map, or bind, whichever one you want to call it, and .ap, those three functions have a specific set of ways that they work together that fulfills what are called the three monadic laws. And I'm not even gonna tell you what the three monastic laws are cuz I don't know what they are, and I'm not a mathematician.

[00:06:46]
But it is important that monads behave according to those laws. If they don't behave according to those laws, all the math is lost, all the benefit of this is loss. There's no point to it, unless you're getting all of that mathematical characteristic out of these operations. So that's why you go to the extra trouble to wrap something like the string Kyle in a monad.

[00:07:09]
Is that you're gonna get these higher level mathematical principles as you start doing bigger and bigger operations. We're not gonna talk about those bigger and bigger operations, partly because I don't even fully understand the bigger operations and partly because that's not the objective of this discussion. I am not trying to make you masters of the nomad.

[00:07:28]
The only thing I wanna show you is that a nomad is not some mystical thing that you can't possibly understand. It's just an object wrapped around the value with some specific behaviors on it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now