This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.

Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Either/IO Exercise 5 and 6" Lesson is part of the full, Hardcore Functional Programming in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian wraps up the exercises covering Either and IO by showing the audience the solution to exercises 5 and 6. - Problem: - Solution:

Get Unlimited Access Now

Transcript from the "Either/IO Exercise 5 and 6" Lesson

>> [MUSIC]

>> Brian Lonsdorf: Finally this is the last example. So get this one. Okay, so this one is just like the others. I think. Let's say we want to get the Href, which is a secret io action that's going to getHref, and then get the protocol. We'll just split it and grab the first part of that.

[00:00:28] So I think just like every other one, we're just going to map get protocol and getHref. I work from five, five worked. Cool. Just another example of the same thing. So, is this getting boring or is this still interesting? Because it's still interesting, I want to talk about it [LAUGH].

[00:00:54] I feel like it should be boring at this point, though. We've got this IO which is a map over it, done.
>> Speaker 2: Mims W was wondering just for clarification sake, that the implementation of left out map seems to not do anything. Is that right? Whatever function you put on it left to just-

>> Brian Lonsdorf: Just ignore it.
>> Speaker 2: Brings itself back out basically-.
>> Brian Lonsdorf: That's why this error message bubbles up through the rest of your app and shows up at the end. It says I stopped running your app because of this [LAUGH].
>> Brian Lonsdorf: In exercise six, this one's a little bit trickier.

[00:01:28] Let's just quickly run through it. Ha ha. Christian's got it. We got maybe here and we got an IO here. So let's talk about this one. This one's an interesting one. So we want to return the maybe email from the cached user. We put the user in local storage.

[00:01:52] Json stringified this user, and so what we want to do is get him out of storage and get his email, because it's George, and he's a guy. [LAUGH] So we start with compose kinda figure that out by this point. So we'll get cash null returns us an IO so we can't use it yet we have to map over it.

[00:02:15] So we're gonna map over it. But we wanna get this email right. So we'll get the email. And most of you might have done that, because every other example is exactly like that. But there's a problem. Because, well, first of all, it's JSON stringified, so you can't get the email on it, so I want to compose that.

[00:02:36] Right? So let's compose that and say getStringEmail as the composition of get('email') and JSON.parse. So I'll map(getStringEmail). But even then it doesn't work and the reason for that is that maybe comes back. For it's an IO of a maybe. So I have to map the IO, then map the maybe to get to the email.

[00:03:05] That's kind of wild. Let's look at that and I get unexpected identify.
>> Speaker 3: Comma, comma before getCache.
>> Brian Lonsdorf: [SOUND] Thank you. Okay, but if I just did this, if I just mapped getStringEmail, getStringEmail's gonna get a maybe given to it, because that's what's inside the IO is a maybe.

[00:03:31] And we don't want to meet we want to JSON parsing it will be like what. [LAUGH] So we actually have to map map map over the IO the map over the maybe we're composing functors we have functors and functors contacts and contacts. If I wanted to write that, I could say this things type is the composition of maybe and IO.

[00:03:56] Go what? Well, it's actually an IO, maybe. The maybe of IO. Put it in an IO, then put it in a maybe. No, maybe IO, okay. [LAUGH] Is an IO and maybe and I can map over that I have to map map because I have to get into the IO.

[00:04:19] Then get into the maybe. IO takes care of the first map it say okay, I'll run you maybe says okay I'll run you if you're there. So we've composed behaviors lust like I want you guys to think about this for a second. The first part of this class was about composing little bits of functions and data to make and app up.

[00:04:40] Now we're composing full on null checks and input, like pure IO, and error handling. We're about to compose more of these behaviors, but I hope you can see that. We're actually composing things that don't feel like they could possibly compose like a. You know like a null check like if you know like well how does that compose of anything.

[00:05:04] Boom. It does, so lazy computations, null checks, error handling, etc. Jessie says I don't have to map map. Or is he talking to somebody else?
>> Speaker 2: He's talking to Luke saying, when I have to map map, or map map map, I'm going to question the usefulness of what this is.

>> Brian Lonsdorf: Yeah. And I'll talk about. You can solve there's monad transformers and you can use lenses. There's all sorts of different ways to do that. You're not going to be stuck with map all the time. But what I'm saying is and in fact, it doesn't matter. The point is that you're composing behaviors.

>> Speaker 4: Is that what flat map would be?
>> Brian Lonsdorf: Almost.
>> Speaker 4: Almost.
>> Brian Lonsdorf: Flat map. Works exactly that way if they're the same type, so same functor. And we'll talk about that next, but first we have to learn. [LAUGH] We have to learn two more functors and will go quick.

[00:06:03] I hope this example wasn't too quick. But for the most part it's just compose this function a map over it. Compose this function a map over it etc and so on. The only difference is IO has a special thing run it and this one has an IO with a maybe in it which is the same junk composes function and that map over it, [LAUGH] You see.

[00:06:26] Anyway