Transcript from the "Functors Exercise 3 Solution" Lesson
>> 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.
[00:00:25] 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 [LAUGH]. 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.
[00:01:16] 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. [LAUGH] 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.
[00:01:52] 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.
>> Speaker 3: [LAUGH]
>> Brian Lonsdorf: Come on ramda, cheering for you. Example two is not defined. All right, get rid of that.
[00:02:07] 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.
[00:02:31] 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?
[00:02:51] 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.
[00:03:12] 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.
[00:03:29] 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.
[00:03:53] 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.
[00:04:09] 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.
[00:04:30] 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.
[00:04:47] 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.
[00:05:16] 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.
[00:05:36] 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.