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 "Functors Exercise 2 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 walks through the solution to exercise 2 and answer a number of follow-up questions from the audience. He also further emphasizes how implementing a map on a container abstracts a function's application. - Solution:

Get Unlimited Access Now

Transcript from the "Functors Exercise 2 Solution" Lesson

>> [MUSIC]

>> Brian Lonsdorf: Let's go over two and then let people do three, basically, just kind of did it but let's look at this. Let me make sure if I run it, remember not to find. Good. [LAUGH] All right. Undefined is not a function because I have not defined the function, let's do that.

[00:00:23] So what my test is hey, I'm actually expecting identity of do, which is the first thing in this list. And I'm gonna run it with xs, which is an identity of this list, right? So, you don't have to think too much about it. We wanna say, use head to get the first element of the list, but we're going to map over identity to do that.

[00:00:45] So, we're gonna map head over the identity. Well, identity gets passed in. Is that what's tripping you guys up?
>> Speaker 2: It was tricky for me [INAUDIBLE]
>> Brian Lonsdorf: Okay.
>> Speaker 2: I tried with actually using [INAUDIBLE]
>> Brian Lonsdorf: Yeah, that makes a lot of sense. The reason I wanted to do this exercise.

[00:01:10] They're all making functions that are not actually running functions and I'm running the function in the test which I probably should have thought about a little bit more, but what's cool about this is it doesn't have to take identity. You can take maybe, you can take a list any functor.

[00:01:25] So of course, the functor has to have a list inside it or else have a blog but mapping over any functor, map is just going to dynamically dispatch on any functor it gets. So, when I give an identity of this it'll work. So, let's run that. But yeah, I could totally understand why that tripped a lot of people up.

[00:01:45] What did I do wrong? Map head, great, yeah. Sorry about that. Did I not do one? We're gonna do one together, why not? [LAUGH] Sorry guys, did I miss that?
>> Speaker 3: No, you did one already,-
>> Brian Lonsdorf: Okay.
>> Speaker 3: And then you blew it away.
>> Brian Lonsdorf: All right. That's right, okay.

[00:02:03] So we've got one in CU Ged. So, we're just going to map out one with the map head. It's really not that difficult, it's just like working with lists, but it's a different type, and you're like why does that work? I don't like it. [LAUGH] Okay, got number three, I like it.

[00:02:22] So for a lot of people online, I don't know if there's a lot of people in the chatroom or what, but if you're out there and you're confused I'm here now and paid. [LAUGH] So, ask me. Questions or Joe? Wait another minute or two before we do three.

[00:02:44] And then, how many exercises are there?
>> Students: Four.
>> Brian Lonsdorf: Okay, cool. I think one of them has like six. [LAUGH] We don't have to do them all. Yeah.
>> Brian Lonsdorf: So we're using currying partial application of map here with head, so I could write this another way to say

>> Brian Lonsdorf: And that'll get me,
>> Brian Lonsdorf: That'll get me an identity a blah, but up here we just partially applied it without calling the whole thing and they could just use xs which is an identity of this list. So, this will actually run it. This will return me a function that takes an identity or some factor.

>> Brian Lonsdorf: Take this opportunity to talk about how much I love the Gin Blossoms.
>> Brian Lonsdorf: Just telling Joe, came on in the grocery store the other day, I was like, yeah [LAUGH] and now I hate what I've become,
>> Brian Lonsdorf: James, head is in ramda and we've been using ramda this whole time.

[00:04:35] There is maybe one other function we might see from it that hasn't been in the other examples. Sorry for not going over that, it's just like first, you have to remember creators in the chatroom.
>> Brian Lonsdorf: I have a 20 second delay, right?
>> Speaker 3: Yeah.
>> Brian Lonsdorf: All right
>> Speaker 3: So when you see they're chatting, they're gonna hear your answer.

>> Brian Lonsdorf: Do that, don't do that. [LAUGH] 20 seconds later.
>> Speaker 3: Someone's asking, what is [CROSSTALK]
>> Brian Lonsdorf: Well, let me talk about, for the people who are bored and, well other people are working, there I'll ask theoretically here. Map lifted head into a functor context. You've raised this function, they just worked on something normal into something that worked on a functor, so we've transformed this function into a new function.

[00:05:40] That's kinda cool, just like a list, if I have a function that just gets like I don't know, running out of cheap easy examples. [LAUGH] But let's say, okay, let's just replace a's for b's and var abs. That's absolute value isn't it? [LAUGH] A4b, take it's s. I don't know, ramda get on it.

[00:06:10] Why isn't this in there? [LAUGH] Should be there. Anyway, so a4b, this just works on a string, right? If I wanted to work on an array, I could lift it into an array function by just boom. Now, a4b's works on an array not just one value. We've broken the problem down into each element we're just gonna run it on the one thing and then we'll just map over it.

[00:06:40] If we need to, now we've got double purpose. I could just call this in line. A lot of people don't do this right now cuz they have the ceremony of the glue code in the names in the return in the blah, blah, blah. I don't know, it's something like Bill Cosby and your jello.

[00:06:54] [LAUGH] But the point is that map, if you just surround a function now, you just partially apply it. Boom, now, this function works on anything.
>> Speaker 5: That could work on maybes?
>> Brian Lonsdorf: That works on maybes, identities, anything. So, hey, I need a null check. Wrap this thing in something, now it's equipped with nulls, null checks, if it gets a null, maybe.

[00:07:15] It works on functors. Depending on the functor, this will run many times, this might not run. This will run with no effects at all.
>> Speaker 5: So when you use the term lift, you're just saying take a standard function and make it work with any form.
>> Brian Lonsdorf: Yes. Yes, sorry, someone, Mark G's talking, I always have to pay attention.

[00:07:38] [LAUGH] So, that's exactly right. Look at the implementation of map on Container. Look at that, where did F go? It went inside of the container. We've lifted it into the container and ran the function from within the container. Same thing with maybe. Let's look at maybe. Hurry, hurry [LAUGH].

[00:08:07] But this one is more interesting, so one would look at this one and say, we've lifted capitalize into the maybe context to run it there it doesn't run here. It doesn't run capitalize on flamethrower, it runs capitalize on flamethrower inside the maybe we've lifted it in there. [SOUND] [LAUGH] So the power of running a function went from within a context, allows you to abstract function application.

[00:08:42] You can do all sorts of crazy stuff. You can do type conversions and you can run many times like a list. You can do maybe, maybe not. Now you have to obey the laws, which we will look at and that kinda restricts what you can do here. But when you get the idea of map, you get a pretty good intuition of it's map, I'm just gonna go in there and map it.

[00:09:06] So, if you're working in Schola and you get an option. You're like I want the thing inside the option. You can't get the thing inside the option. You have to map over it. So yeah.