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 "Functor Laws & Properties" 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:

It's also important to recognize the laws and properties that functors must follow. These laws and properties make functors flexible and pluggable into any application. Brian discusses these laws and properties and shows a few illustrations. He also presents a number of scenarios to the audience and asks them to describe what functors would be required to build each scenario.

Get Unlimited Access Now

Transcript from the "Functor Laws & Properties" Lesson

>> [MUSIC]

>> Brian Lonsdorf: So you've seen Promise, maybe EventStream, future IO. It's just map. I don't know why people have to make up their own names and APIs. It's kind of frustrating, especially when map has laws and intuitions and a whole mathematics behind it and people are like, I don't know, subscribe.

[00:00:23] [LAUGH] You're like, God. And so there is a way to do it that will guarantee that your library composes with other libraries and there's a way to do it to where everybody doesn't have to read your documentation, they just kinda know. Didn't you guys just kind of know how to use event stream and IO eventually after you just see the map?

[00:00:44] Maybe it's returning something, was map over it, whatever. That's all it is. So you feel it, you know what's gonna happen, it's gonna be returning a new IO with transform stuff or whatever. These laws and properties are actually useful in your day to day code, there's a ton of them.

[00:01:02] Functors have laws, these are laws that you have to obey when you're making a functor. And you know that identity and composition? I don't know if you remembered but there's this thing about categories and compose and category theory, those are the same laws just in a different kind of way.

[00:01:23] The other had left and right identity and sensitivity. This is kind of like the functor playing off of those laws' laws. So it's connected to compose in some way and we'll learn more about that if I have time. [LAUGH] Somebody give me a Redbull. Here's just a quick look at something interesting with the laws.

[00:01:43] I know that this diagram on the left is just a cute drawing from a great blog post that kind of shows a more formal category theory diagram might be. If I have a functor or if I have just have a normal function from a to b, and those could be any type.

[00:01:59] So, in this case, that's reverse, string to string. A is string. B is string. They don't have to be the same type and that's why it just happens to be. Now, if I run reverse on bingo and then I put it in my array, I get ognib. [LAUGH] And if i first put it in my array, then f map over, fmap is map, I get functor map whatever, I get the same result.

[00:02:24] Both take me to the same place, a to t fb, t is our functor, b is inside it. So I get our array of string inside of it, very interesting stuff. It's there for you to learn. They're in the slides, you can look back at them. Here's another really interesting idea.

[00:02:43] We could actually do that composition and if you do a map map, that's the exact same thing as mapping composing. If I go back to this law at the bottom, it says if I compose map map, that's the same as map compose. And if I see that, literally see that line in the code, I could always change it.

[00:03:05] You guys were writing that a minute ago, compose map partially applied with F. I could just do map compose, partially applies. So that is actually useful. It's right there in front your face. So the bottom one is way more efficient. If I have array of a billion things and I map over it, and then I map over it, that's gonna be really inefficient.

[00:03:29] But I know I can always just map the composition over it once. Natural Transformations, tidbit of information, shouldn't waste our time on it. But essentially you can take one functor to another functor, if anybody cares about a natural transformation. A lot of category theory cares about that. You can turn it maybe into a list just by making a singleton list or a list with nothing in it.

[00:03:53] So that's a natural transformation from a maybe to a list. And there's more laws [LAUGH] and more diagrams. And it's really just connect the dots. You guys remember the dots, they're just like, hey, this function goes to that dot and that function goes to that dot. I can cut out the middle man and just go straight to that other dot.

[00:04:09] I don't have to worry about all the intermediate steps. This is that kinda of stuff but with contexts and wrappers and mapping and it all relates. I'm not gonna go over like, it's not that hard, you can look back at the examples. It's all very easy stuff but once you build up and you just kind of know these simple truths.

[00:04:28] Okay, we're gonna do a fun game to break it up. [LAUGH] Everybody's like, yay. [LAUGH] All right, so if I want to make an API call with an ID and possibly retrieve a post, would functors might be part of that? Does anybody have an idea, just shout them out?

>> Students: Maybe.
>> Brian Lonsdorf: There's a maybe. And what about the API calls, can anybody? So was that IO?
>> Speaker 3: IO.
>> Brian Lonsdorf: IO's good. Also do a future. I would've chose a future because future because has IO in it but if it was a synchronous call, IO would've been perfect.

[00:05:07] So I'm gonna Future of maybe, these aren't right. You can do different things. You could've been, possibly retrieve it a post all you do neither with an error message. I'm not saying this is the right answer. I'm just saying it's cool. You can mix and match these cards.

[00:05:21] How about I click a navigation link and insert the corresponding HTML onto the page. So I'm gonna click a link and then we're gonna set the HTML and we're gonna build the page and put, so we'll get this.
>> Speaker 4: EventStream.
>> Brian Lonsdorf: EventStream, that's good. Anybody else?
>> Brian Lonsdorf: An EventStream and IO.

[00:05:44] There you go. Easy breezy. Putting it on the page is definitely an IO, EventStream is lazy nothing, happens to subscribe. But when you do subscribe, you want to do some IO, right? Or you don't care, that's fine too. [LAUGH] If you don't want your thing to be extended and that's the end of the line, just put it on the page and you don't worry about it.

[00:06:05] One last one. Hey, if we want to submit a form return errors or make a API call that will create a user. Let's see, did I word that correctly? [LAUGH] Submit a form, return errors or make the call.
>> Students: [INAUDIBLE]
>> Brian Lonsdorf: I like that. The event stream could be the submits on the form and then the either could be the I think I did just a, hey, I did that.

[00:06:37] We've got a event stream in each future and now notice these are different types, so we're gonna have to map, map, map that. And it has to be kinda clear cuz you're saying, listen guys, I have three different functors, three different contexts here that are nested. Now we talked a little bit briefly about how you don't have to map, map, map, you can define a lens to do that.

[00:06:58] I don't have time to go into them. But it's pretty great that you can map three deep in contexts.