The full video and many others like it are all available as part of our Frontend Masters subscription.

Brian Lonsdorf

Brian Lonsdorf has taught several workshops and training courses on functional programming with javascript. He'a a regular speaker at conferences and helps organize/host FP events around the Bay Area.

Brian Lonsdorf

SFJS Co-Organizer

Exercise 3 asked you to use the safeGet and _.head functions to extract the first initial of the user. Brian walks through the solution. In a later video, he comes back and further explains this exercise.
- Problem: http://jsbin.com/yumog
- Solution: http://jsbin.com/xiwofojura

Get Unlimited Access Now

Brian Lonsdorf: Let's do exercise three together and four is kind of a bonus. I should have put a star. I put a little star next to the bonus ones, but it's not that hard. I'll let you guys work on four, but it's definitely harder, because you have to write the maybe yourself, like call it yourself.

So we use safeGet and _.head again here to find the first initial of the user. Did anybody have any snags on this before I go into it? What's that? Did you get a snag on it?
Speaker 2: Yeah.
Brian Lonsdorf: What did you try? I'll write what you tried, and we'll talk about it.

Speaker 2: So, I wrote map, and then _.head comma safeGet.
Brian Lonsdorf: Comma-
Speaker 2: SafeGet.
Brian Lonsdorf: SafeGet. So close. We're, whoa, not my computer. We're mapping the _.head function over the safeGet function here. You're so close, we just have to compose them. Because we want to first safeGet then get the _.head.

Is that the answer? Let's see.
Speaker 2: Compose, then map, then _.head.
Brian Lonsdorf: What did I do? Let's see. Use safeGet-
Speaker 3: For safeGet, I gave it a parameter of name, to tell it what-
Brian Lonsdorf: Thank you guys. I think I have a-
Speaker 3: Now see, now that's what I have but it doesn't work.

Speaker 2: Yeah, I get the same script error.
Speaker 3: Yeah.
Brian Lonsdorf: Hold on, let me make sure.
Speaker 4: I wrap the compose around everything.
Speaker 2: Yeah, I did a wrap compose around a map of the _.head.
Speaker 3: And did that work?
Speaker 2: Yes.
Brian Lonsdorf: Okay. Sorry, guys. That's right, I'm backwards.

So let's talk about this.
Speaker 3: Yes, because I'm confused.
Brian Lonsdorf: Because I had a syntax error up there. Yeah, you should feel like a champion. This is a good one.
Brian Lonsdorf: Come on ramda, cheering for you. Example two is not defined. All right, get rid of that. There it goes.

Okay, let's talk about this. Wait, expected, is this 3 3 okay. Okay cool. So let's talk about this. By the way, just so you guys understand, I've got my exercise and description, some helper stuff and data and then it's like start it, and just this is where it starts and then the test.

It's like that format on every single one. So when you see the ex3, that's usually what you have to work on. The rest is just kind of noise but helpful. So yeah, let's talk about this. What does safeGet do? SafeGet works just like get, but it put it inside a maybe, right?

Because you might not have a property. This user might not have a name. So, if I call safeGet('name') on user, it's going to return me a Maybe right here. Do you guys see right here? We'll have a Maybe of ("albert"). All right, that's what's happening after the safeGet.

And we have to map _.head over that, we can't call _.head on the maybe, we have to call it on the string inside the maybe. So we map over the maybe Albert. So, the point is that the first function returns us a functor. The we map _.head over that functor.

One cool thing to note is if I just do null here, It's going to fail the test but it's not going to blow up. Expected Maybe(A) to match Maybe(null). It never ran _.head. Now that'll save you giant stack traces. Whole things like let's say, you don't have a current user and you don't want to display their name if there's no current user.

Drop them in a maybe and map over it. I don't know if they're there or not. But if they're not, I just won't run this. So, you essentially put it in the box, like the maybe box and say, I don't know what's in this box it may or may not be there.

When you map over it, it may or may not run, but that's very safe as a programmer to force the rest of your code to deal with the fact that thing may or may not be there. Things can't just run on it anymore. It has to map over it, and you get a right time safety that you don't get unless you do that.

The rest of your code will now not blow up from a random null. Or if you wrote a library and you returned a maybe that one time that it returned a null and just blindsided the app, like they would have had to deal with it. They would have had to map over it in the first place when they wrote it rather than just getting hit with a null later.

Technically, compost.
Speaker 3: Yes.
Brian Lonsdorf: You can, you can map, no, no, no, you can't, because it is totally Schrödinger's cat. You can't map over the safeGet because it's not a maybe yet. SafeGet takes a user and then returns a maybe of its name. It's not a maybe going into it.

So, you don't have anything map over you're just getting a user. Do you guys see that right here? When I compose what's coming in, it starts out as a user right here and then after it gets through the safe maybe, or safeGet, I have a maybe of user right about here.

And then I have to map over that and get the maybe. So, now lets get back to Albert. Okay, let's do the last one and talk more about functors. Because we got to know them.

Ready to take your code to the next level?

Intense courses with world-class teachers and unlimited access to our growing library of videos for the great price of $39 per month.

Get Unlimited Access Now