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 1" 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:

In this exercise, you will use _.add and to make a function that increments a value inside a functor. Brian does this exercise with the audience to help get them started writing functors. - Solution:

Get Unlimited Access Now

Transcript from the "Functors Exercise 1" Lesson

>> [MUSIC]

>> Brian Lonsdorf: I lied, container's actually called identity. So, we're gonna look at identity here and just pretend it's container, okay? Same thing, different name. Okay, so let's do exercise one together. I run mine a little bit differently, but same idea. We've got our test down here that's going to run our example and pass or not.

[00:00:34] So we have to define how how to, yeah, yeah, we're gonna use add and map to make a function that increments a value inside a functor. Does anybody want to give me some help? Does anybody kind of get this stuff?
>> Brian Lonsdorf: [LAUGH] All right, let's try it. Okay, so, and I strongly encourage you to just open this up and just start playing with it, start wailing on it.

[00:01:03] Like over here, I could say, what if I have Identity of 3? It's this big gross thing, all right. Let me do toString on it, so it looks nice. We have an Identity of 3, literally. How do I add 1 to that? I can't do add 1 with Identity of 3, right?

[00:01:23] That's not gonna work. [LAUGH] cuz add is not defined. Now I get, 1Identity(3), that doesn't work. I want to add 1 inside the Identity. We learned about this map function a minute ago. So that's probably what it's gonna be. Does anybody wanna give me a little help at this point?

>> Brian Lonsdorf: Okay, we'll keep going. I want us to understand this, so don't let me go on until we get this, all right? Somebody's asking what pointfree and JS Bin is. Pointfree is a library that implements the fantasy-land spec that we'll talk about later, but in a pointfree way.

[00:02:06] Hey, hey, Valentine's has got it. So, am I saying your name right? [LAUGH] Like the saint, right? So we wanna map(_.add(1)) over Identity of whatever, I'm just writing it here so we can see. So that'll be, see, it looks a little crazy like that, let me do a toString here.

[00:02:29] JS Bin outputs the whole thing for some reason. If you did it in the console, it would look a lot nicer. But we're gonna get Identity(4). This isn't the solution, I want a function that increments a value inside any functor. Well, there it is, I'm just going to map(_.add(1)) over whatever functor comes in.

[00:02:51] So let's try this out, see if it works. Okay, exercise one is okay. So let's talk about that, let's beat it up. Ask me questions, tell me why I'm wrong. Let's talk about it, [LAUGH] anybody? You there in the hoodie. [LAUGH]
>> Off Camera 1: [LAUGH]
>> Brian Lonsdorf: He's not a plant.

>> Off Camera 1: How is it, I thought map took a lot of arguments?
>> Brian Lonsdorf: Yeah, no, no, it's curried. So we're only giving it one right here, just like add takes more than one arguments, and that's curried. So we're mapping curried with add. Normally we'd do it like, second argument, Identity(2), etc.

[00:03:32] So that's a great question. We just went over that in part one. [LAUGH] But part of, part of map, part of what everybody is hardwired to think map does is run many times, right? Everybody's like, map will run many times. [LAUGH] And it doesn't. If you have an array of one item, it runs once.

[00:03:53] If you have a container with one thing in it, it runs once. If you have a maybe with one thing in it, it's gonna maybe run that once. So think of it as, map is not about iterating. It's about going inside of a object or a data structure and running a function from within that data structure on its properties or behavior.

[00:04:18] Identity will become a little bit clearer later. The main thing to think about right now is just like a neutral functor that you can play with and so you get a feel for map, with no added behavior. So let's do another one, let's see. So I'm going to map this real function, this is a little bit clearer.

[00:04:38] So I'm going to get x, and x will be whatever's inside my identity, okay? And I'm going to do x.toUpperCase(), awesome. This is gonna be the best. [LAUGH] And then I have my identity. Ident, I've lost how to spell identity. Identity, yeah. Of, let's do someString, all right.

[00:05:08] And, that didn't work. Gotta return, maybe?
>> Brian Lonsdorf: Yeah, gotta return. Okay, let's do it over here on the left.
>> Brian Lonsdorf: Yeah, this is awesome.
>> Off Camera 1: You should do it [INAUDIBLE].
>> Brian Lonsdorf: Yeah, I'm just gonna do over here and like name it. Do it, thing. All right, so it takes x.

[00:05:36] In this case, let's make x firehose to commiserate. And then we'll return x.toUpperCase(). All right. Now we run this. And you know what I'm gonna do? I'm gonna call toString on thing at the end. Thing, thing isn't, thing is already there, yeah, thing.toString. This, seriously, having a call toString is not common, it's just because we're in JS Bin.

[00:06:13] Is this too low on the screen for everybody?
>> Off Camera 1: Is it too what?
>> Brian Lonsdorf: Low on the screen to see?
>> Off Camera 1: It's fine.
>> Brian Lonsdorf: Okay. So let's try a different one. So x became, let's give ourselves some room here, x became firehose inside the map, and it worked all inside Identity.

[00:06:35] So mapping this function over identity of a firehouse. x became the firehose, we uppercased it. And then what we get is Identity with the uppercase FIREHOSE in it, neat. [LAUGH] So we ran a function inside of identity. Let's think of another example. Let's do, we'll do our array, why not?

[00:06:59] 4, 3, 2, 1. And we'll just do reverse on that. The destructive impure reverse [LAUGH]. So we'll do our thing.toString. There it is, it reversed it. Are you guys getting a little bit of a feel for it? Are you playing with it? Or are you just staring at it and trying to understand why the hell map is working on the Identity?

>> Off Camera 2: We're having some confusion in the chat on what thing is, what we're going over with that.
>> Brian Lonsdorf: I'm sorry. It is our test function we're defining to play with, because this is kind of hard to work in, so I'm gonna work over here and then just run it over here.

[00:07:45] So it's actually not our test function, to be honest, it's our, there it is, now it's a function. So if I call test function with identity, yech, toString. Okay so, let's see, let me think of one more example here. Because we've gotta, I've gotta let you guys play with this.

[00:08:14] I gotta cut you free. You have to fly [LAUGH]. Somebody asked, does Identity act as a list within a map call? No it doesn't. There is no array going on here. This is a totally different map than you're used to. So identity is just sitting around something, anything.

[00:08:39] Let's make it, does anyone know the date, getDay or something like that? All right, so now we're gonna map this over a new date.
>> Brian Lonsdorf: See if that works. [LAUGH]
>> Brian Lonsdorf: That's weird, what'd I do?
>> Off Camera 1: Did you run it first?
>> Brian Lonsdorf: [LAUGH] good old. Wait, no, but it should already have run the map over the identity of the new date.

[00:09:15] Did I do it wrong? Maybe I have to re, I see, you've gotta run it to refresh it. toString of undefined. I don't know what the actual date getDay function is, it doesn't matter. The point is that Identity has anything inside it. It could be a date, x becomes the date.

[00:09:33] It could be a string, x becomes a string, it could be whatever. Identity is like a list of one thing. So if I have 2, I could make 2 plus 2, right? x becomes 2. Does everybody see that?
>> Off Camera 2: Hey, Brian, there's a question from James M in chat on whether this, I think he means identity as kind of like a with block in JavaScript or Python.

>> Brian Lonsdorf: Don't think of that way. You're just going to destroy your brain. [LAUGH] I mean, if that's the way that makes sense to you, sort of, but I guess you can see that you're getting the context. It's not going to change this, if that's what you're worried about.

[00:10:17] We're not gonna use this at all, which is pretty great. Except for in inside the implementations of objects, which you don't have to deal with. Yay, no this! [LAUGH] So let's run this and try to do this with a list here, just make sure. Okay, I get a 4 out, because I called toString on it.

>> Off Camera 3: I got a question, Brian.
>> Brian Lonsdorf: Yo.
>> Off Camera 3: Are you showing Identity just because it's the easiest functor? Or is it something that you actually use much compared to maybe [INAUDIBLE]?
>> Brian Lonsdorf: Identity is cool because what it does is allow you lift things into the functor world. Even if you don't want any side effects with it, it can compose with other functors now.

[00:10:58] It's kind of the basis of monad transformers. And you end up using it just like id. Like, when you see the id function, you're like, what would I ever use the id function for? [LAUGH] And then you actually start doing pointfree code, and you're like, I need that.

[00:11:12] So yeah, it's just like this neutral functor if you need a functor and you don't want any side effects to satisfy some type signature. Or let's say some jerk made their function with map around it, without making it, calling map later.