Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Functors Exercise Wrap-up" 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 by answering a few more questions. He jumps back to exercise 3 to step through the solution more thoroughly. He stresses that functional code doesn't have to be completely point-free. Maybe object can be mixed with imperative code. - Problem: http://jsbin.com/yumog - Solution: http://jsbin.com/xiwofojura

Get Unlimited Access Now

Transcript from the "Functors Exercise Wrap-up" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Speaker 1: Hey, Brian.
>> Brian: Yo.
>> Speaker 1: You've got one question that asks if there's any way to get the value out of a functor?
>> Brian: That's a great, great question. Let's move on-
>> Speaker 1: He requests to go over exercise three again cuz someone's still stuck on it.
>> Brian: Someone's still stuck on exercise three?

[00:00:25] Let's see.
>> Speaker 1: You can go back to yours if you don't want to on mine.
>> Brian: Yeah, here we go. Let's do that. I'm gonna talk about getting the value out. In short, no. Why if it wasn't there would it be there later? [LAUGH] You're like, this might not be there.

[00:00:44] And then later you're like, it's cool, it's there. Boom, right? If you're going to put it in a maybe, it's not just gonna materialize later. If it's null here, it's gonna be null there. And the point is, the reason for that is not like you're mapping functions over it.

[00:00:59] Those functions won't run, it just doesn't do anything. It just skips your whole app at that point. Every time you map over, it just doesn't do anything. So if you have a value that may be null, and you put it in a maybe, you don't take it out because you're just putting in something and taking it back out for no reason.

[00:01:15] You've lost all the benefit, you keep it there to let everybody know it's there. And then eventually, I'm answering the question about, do you get the value out of the maybe? Or how do you get the value out of the maybe? You will eventually do something with it, whether it's put it on a screen, log it out, fill in the URL bar, something, make an API call.

[00:01:39] Something will happen with the value inside the maybe. So when it gets there, you either run it or you don't. But you don't take it out in the middle of your composition. I was gonna go over three again, right? So let's do a step by step one of this again.

[00:01:59] All right, so example three asks us to do the same. I'm glad we're doing this, by the way. This is the hardest part of the class, right here, right now. That's why I was so serious about us knowing this, asking questions, spending our time on it. There's gonna be a ton more functors.

[00:02:16] There's nothing you need to know more than I just map over it. Done, done, done. So all right, we have safeGet and head, and we're gonna try to find the first initial of the user. So what's happening? In our exercise three, we're getting the user, which is this data right here.

[00:02:35] So we're gonna use safeGet to get its name. So I know I need to call safeGet with name to pull the name out of the user. If you hadn't been doing the other ones, we have this get function, and this is just the safe version of that, because it returns us a maybe.

[00:02:53] Because it might not be there whenever you pluck the value, or get the value, or grab the property of the user. This name might not be there, it might be null.
>> Speaker 3: You said in the console, the one you showed.
>> Brian: Yeah, here it is, safeGet. Name on id two, three.

[00:03:15] There is no name there, ours, SafeGet. Do I have to run it first? There it is. We got a value of undefined. Let me call to string on that so you guys can see it better. These two strings shouldn't be normal. Just want you guys to know that.

[00:03:33] It's just because we're in JS Bin, but if we have a name. I don't know, Gman. [LAUGH] So,
>> Brian: Let's do my name.
>> Brian: So maybe Brian, so we've got this user with the name Brian, and we got the name. It comes out as maybe Brian. But if I gave the user with no name, I got a maybe null.

[00:04:03] So safeGet, we'll put it in a maybe because it knows it may or may not be there. That gives us a way to tell the rest of our program and the rest of the programmers and our future self, this thing is going to be null later, and I've marked it.

[00:04:19] I put it in this box that says now everything's impossible for you to understand. So just kidding. [LAUGH] So we have safeGet. We've got the name. We've got this maybe Brian. Or maybe Albert, but what do I want to do? I want to call head to get the first initial of that user.

[00:04:39] All right, cuz it'll grab the first thing, so I want to call head on that. Well, that doesn't work, because safeGet is a maybe Brian. I can't call head on a maybe Brian, that doesn't work. I don't even know what that does. But I can map over it, there it is.

[00:05:01] I get b, again with these two strings. So it mapped Ted maybe over Brian, and the way that gives me safety is if I map head over maybe null, nothing happens. If we call toString, maybe null, it's fine, doesn't break. [LAUGH] So instead of head, I have to map head over that, right?

[00:05:28] And something that's been tripping us up throughout. But I'm glad, because, yeah, [LAUGH] I'm glad because you guys are gonna have to hit this a lot if you're trying to write programs like this. This is probably not the way to do this. You wanna partially apply map and compose these things together, and it'll be run later.

[00:05:48] Everything will be run later by the callers. You just wanna build up your app of functions glued together. You don't wanna be running things left and right while you're making your app. There you go. So that's our solution. So I wanna address one thing before we go on.

[00:06:05] And that is that I was just, what was your name? I forgot, the blue.
>> Phil: Me?
>> Brian: Yeah.
>> Phil: Phil.
>> Brian: I was just talking to Phil, and we were talking about, so, first of all, almost always, my app is just a bunch of functions, right, glued together, whatever.

[00:06:25] And I'm gonna get one piece of data from outside. I'm not getting 16 different inputs coming into my function. I'm just gonna get something. For instance, let's say I'm gonna get an API call. Let's do API.get /users/3. So that's my input to my app. So I did an API call to get a user.

[00:06:51] Now I have data in my app. My app's come alive with this data. I'm not getting, that's my entry point, and once I've got my data, let's say that user might not be there. I'll just drop them in a maybe, or her in a maybe, and then I'll map the rest of [LAUGH] my app over it.

[00:07:14] Maybe that's UsersController, whatever. But the point is, once I map the rest of my app over that, it's either not gonna run at all, or it's gonna run and that's it. So you're not dealing with maybe, man, maybes all over the place. Once you map once, you really go to the rest of your app, typically.

[00:07:38] Now I could do this, too. If I wanted it to, I could map other stuff. But typically, this is what it's gonna look like. You have the rest of your app to map over. If it's null, you don't wanna run it. Now one other thing that we were talking about was, let me put a function here.

[00:08:01] I'd do it the other syntax way, cuz it's easier, right? I'm a user here. Now I have a user, I can be, var u is blah. And I can do imperative code. [LAUGH] But that's what I'm saying is now you have your user for the rest of your stuff.

[00:08:20] If it's not there, if it's there. Well, I guess I have to put this in a maybe, but we'll get to this in a second. But the idea is that you don't have to be point free. You don't have to make it without object or arguments. Your maybe value may be passed in there, and you can run the rest of your app in here, too.

[00:08:39] So I can make some call to UsersController.renderIndex with my user, done. Or if I wanted to get fancy, I could bring it back out to being a first class function. And now we're good. All right, back to the presentation, back to the future.
>> Speaker 5: I had a couple questions about that stuff that you just showed.

[00:09:05]
>> Brian: Sure.
>> Speaker 5: First is, what is API? And then, because the video was frozen, and then the second is, yeah.
>> Brian: Okay.
>> Speaker 5: You can answer that first and then.
>> Brian: API was just a fictional API library I was using to get some user from an API call.

[00:09:22] It's not real. [LAUGH] And the second one.
>> Speaker 5: Paul Kimmel was asking, doesn't the API being generally a sync, break the rules of statelessness?
>> Brian: Hold on, my friend.
>> Speaker 5: [LAUGH]
>> Brian: [LAUGH] You're this close. We're about to learn about functors that run in the future. All right, so hang tight.