This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.
Transcript from the "Monads Exercises" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Off Camera 1: Real quick. We got some confusion here I think. I'm bored, so we're calling fork on the future here but the earlier we defined the fork function where we gave it lastly in the afternoon G and had an x, right?
>> Brian Lonsdorf: Yeah, yeah.
>> Off Camera 1: Different forks.
>> Brian Lonsdorf: Different fork, different fork.
[00:00:24] [LAUGH]
>> Off Camera 1: There's been some confusion there so.
>> Brian Lonsdorf: I think this is my most up to date monad example. But that's okay.
>> Off Camera 2: You had one slash one instead of slash three, which is what we came up with.
>> Off Camera 2: You went to [INAUDIBLE]
>> Brian Lonsdorf: While we. Here you go, cool.
[00:00:54] Perfect. I was like, my God, they're all done. Darn chick. Haha, pretty darn. [LAUGH] [LAUGH] All right, we got like-
>> Off Camera 2: Forward slash one is the answers!
>> Brian Lonsdorf: We are just gonna kill, because you guys know this. All right, lets talk about this. So, we're gonna start with that last example we just did.
[00:01:16] We had a (safeGet)('address') and it's safe and then it map(safeGet of the street. And so, we're traversing the address and the street safely. The alternative to this code is, if not null, do this. And then nested if not null, do this. And then. So we've got our street, and then we want to get its name.
[00:01:43] So we'll, again, map(map(safeGet('name'), and link. And let's see if exercise one is unhappy about something.
>> Off Camera 1: You've got three Ss in address.
>> Brian Lonsdorf: We've got that, and we've got one missing param. And also missing print there. Kicking butt, you guys [LAUGH]. There we go, so expected maybe Walnut Street to you people maybe, maybe, maybe Walnut Street.
[00:02:19] So, this is kind of a mess here with all these maybes. Why don't we say at the first sign of it getting nested, we'll mjoin it.
>> Brian Lonsdorf: And we get rid of one of these maps.
>> Brian Lonsdorf: Let's see what we get for that.
>> Brian Lonsdorf: We're got a maybe, maybe Walnut St. one less, we're getting there.
[00:02:40] [LAUGH] And then we'll mjoin here to clear it all up and there it is, a pass. We just got one maybe. Now we just learned that map mjoin, map mjoin, it's just chain. So it's called chain, here we go. And there it is, but it's chaining through. Let's do another one.
[00:03:10] We've got two IOs. We'll just compose getHref with mapping pureLog. And we will mjoin that, that's all we gotta do, cuz we had to put in another IO. And that passes that. See if we can just call chain here instead, there we go. And the last one says, we've got two futures.
[00:03:41] We're just gonna basically get the post and one thing to know about monads. Is we are nesting calls, so we're saying first get the post, then get the comments, right? So first get the post, then get the comments. Actually getComments takes an id, so we get the id first.
[00:04:08] And then, yeah, that's it. And then we just mjoin.
>> Off Camera 3: Hey Brian, there's a request to slow down a little bit.
>> Brian Lonsdorf: I'm sorry. We're seeing it's 4:11 and we still have a real life example to do and I just, this is the last example. And I broke it anyway here.
[00:04:24] The point of this is you've been seeing map this whole time, right? I mean we've been seeing what map is doing. We're essentially just going inside the future, and now we're running and we're getting another future inside that future, and now we have a future of a future.
[00:04:46] So any time we have a future of a future, we mjoin him and we're all good. And I think this will actually work even though it shouldn't be. Yeah, that works. It should be getting the mapping the address over the post. Yeah, so there's not really much to it besides dropping mjoin at the end.
[00:05:07] Or instead of map mjoin, whenever I see that, I can call chain. And all is well.
>> Brian Lonsdorf: What is it, reading? When reading, can I essentially ignore the chains and mjoins? Well, what they're telling you is saying, we're gonna first get the post. Then we're gonna get the comments.
[00:05:31] Chain is telling you that. You're not just like, get the post, get the comments. I don't know, [LAUGH] it's telling you what's gonna happen. But there is a way to mcompose that will compose two monadic actions together. And let me just finish up before we do the demo by saying, there is more to the story than I'm telling you right now.
[00:06:00] [LAUGH] And Monads have these same laws and same paths to go through and just we're going to find them compose. Do you guys remember these category laws? Where we had functions we had composed, right? And composed had left and right identity and had associativity. And we saw those functor laws are kind of playing off of those.
[00:06:24] Well, Monads form a category. They're their own type of composition, and they have their own identity operator, namely the of part actually should be m.of in the JavaScript spec. But the idea is that you have a category of monadic stuff that has all the same laws that apply to non monadic functions.
[00:06:47] And it's own world and it's got its own rules and you can do all the same things. All those things that we could do with normal functions and compose, we can do that with monads.