Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Functors Exercise 4 Solution" 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 steps through the solution for exercise 4 which requires you to use Maybe in place of an if statement. While solving exercise 4 he jumps into an attendee's JSBin to help them solve the exercise. - Problem: http://jsbin.com/yumog - Solution: http://jsbin.com/xiwofojura

Get Unlimited Access Now

Transcript from the "Functors Exercise 4 Solution" Lesson

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

[00:00:04]
>> Brian Lonsdorf: All right, this one's pretty great. So here's what you're going to see in your normal code. This is like what you're gonna see on your day to day. And we're gonna say if we have a number, we want to parse the end. Okay, well I know how to write that.

[00:00:22] I could say, well, I have parseInt. Okay, and what do I want to do? I want to compose that with first put it in maybe, right? Because it may or may not be there. But then I have to map parseInt over it, and see if this works. Hey, hey it worked.

[00:00:43] Let's make it weird. Things got weird, then they got real weird. Come on, run it. There it goes. Expected 4 equals 43. I just like to fail it sometimes just to know we're on the right track. It's a good little tip. You can just like mess with the test so you know that you did it right.

[00:01:03]
>> Speaker 2: Hey, Brian.
>> Brian Lonsdorf: Yo.
>> Speaker 2: We're getting asked for a more step by step breakdown of how you're coming up with the solutions.
>> Brian Lonsdorf: Sure.
>> Speaker 2: Having some trouble following.
>> Brian Lonsdorf: Okay, sure. So, with this example 4, I've recognized a pattern that says if this value is here, I guess you more likely see this if it's not equal, equal, null, && it's not equal equal.

[00:01:29] But n is not equal equal undefined and we'll see this every day in JavaScript code. People write this over and over and over again. Then we want to do this. And we're saying like, okay, I know that there's a pattern that captures this. I know that's called maybe.

[00:01:53] If I have an null check and I want to run something if it's there, I will use maybe. Well how do I use maybe? Well I know I wanna put my value in it. That's an important part. Functions don't go to maybes, values go into maybes. So if I have a Maybe (2), then if I can't parseInt the Maybe (2), I have to map parseInt the Maybe (2).

[00:02:21] Then all of put a string and we're gonna Maybe (2). So I was just kind of pushed into this solution because I said I have to put n in a maybe because it might not be there. My value goes in the maybe, in that container, and then when I map this function over it, it checks it.

[00:02:42] I realize that the value's in the container now so I have to map this function over it. Now the composition of these two things, putting it in the maybe then mapping parseInt over it, is kind of what we're looking for to gain a function instead of just running it immediately.

[00:02:59] But you're gonna see this a lot in your code, cuz most your code is going to be making functions like that. You're not just going to be running things left and right inside other functions. You gonna be composing and combining things into new functions. All right. So that was 4.

[00:03:19]
>> Brian Lonsdorf: All right. How are we doing? Was that a better breakdown?
>> Speaker 2: I'll ask.
>> Brian Lonsdorf: Break it down.
>> [SOUND]
>> Brian Lonsdorf: [LAUGH]
>> Speaker 3: [INAUDIBLE] Create the logging function again. And I threw a log between the map pareseInt and maybe and there was no output.
>> Brian Lonsdorf: Really? Right here?

[00:03:45] Let's do it. So here's our log function. I think in other ones I've defined this for you just to help a little bit. We'll return to x so it counts a log and return it. Now then we log in the middle. Yeah, this big junk. What is this thing?

[00:04:08] This is the JS Bin specific version of an output of an object that I've never seen in any other interpreter. [LAUGH] But it says our value is 4 and it's inside this giant object. So see, if I call toString on this it'll look a lot nicer. There it is.

[00:04:29] So is it maybe the return x, or?
>> Speaker 3: No, I got that.
>> Brian Lonsdorf: Do you make any adjustment?
>> Speaker 3: Sure.
>> Brian Lonsdorf: All right, let's do it.
>> Brian Lonsdorf: Get this guy. Okay, what's the letters?
>> Speaker 3: K-U-M-A-Q.
>> Brian Lonsdorf: K-U-M-A-Q.
>> Brian Lonsdorf: Yeah, there it goes. Fixes it. It's got some value.

[00:04:53]
>> Speaker 4: I created a arbitrary [SOUND]
>> Brian Lonsdorf: Okay, perfect. So exercise 5 says, so we're gonna log. Yeah, looks very close, like exactly what I have. Let's see. Exercise 5. Log. I'm calling exercise 4. [LAUGH] Sorry, man. That's a bummer. [LAUGH]
>> Speaker 4: That's why you shouldn't grade exercise five.

[00:05:18]
>> Brian Lonsdorf: [LAUGH] Actually, when I was naming these, I was like these are horrible names. Every time we rearrange it, I have to rename every exercise.