Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "Identity Functor" Lesson is part of the full, Hardcore Functional Architecture Patterns in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that functors are monoids, and demonstrates the advantages of programming to an interface. Programming to an interface means referring to a more abstract level than a class.

Get Unlimited Access Now

Transcript from the "Identity Functor" Lesson

>> So next steps here. We're going to look at functors. So let's jump over to, let's save that and go to monoids 2, monoids 1, 0 indexed, of course. So functors are monoids. All right, I'm gonna be using monoids and semigroups kind of interchangeably. I'm really getting at the concatenation behavior.

[00:00:29] And identity comes as a very nice property that follows from that. But most of the time we care very much about concating. Not as much about identity. But it does pay off. So it's good to know. But I'm gonna use it interchangeably. So we have these functors here, ID.

[00:00:50] This was box in the functional programming class. It's the identity functor. It just does nothing. Task capture the async. Either captures the you'll have a left or a right, usually errors. So if I put a task, I'll start with the ID. ID's easy to look at. And let's grab some of these some of these fun monoids that we've defined over here.

[00:01:17] So we've got any and all and empty and whatnot. We're gonna just throw them up there. And there we go. So we have an ID of a sum of 2. And I can actually concat that with an ID of sum of 3. And we should get an ID of the sum of five.

[00:01:39] So ID is a monoid if what it's holding is a monoid. Like to say a butter fingered hang glider. It is one whilst it holds one. So via the fact is that If it's just a semigroup, then ID would be a semigroup. Right, it's whatever it's holding defines its behavior.

[00:02:02] So we could say let's go ahead and see if this actually works. Fingers crossed y'all. And I think I have to call extract to get it out of the ID for now. So let's look at that. Or in fact, let's not call extract and see what it gives me.

[00:02:21] Node monoids one. Yeah, so we get this kind of thing, which is an identity functor. It's just hard to look at. So we'll call extract.
>> And in this case extract is the same as full in the last class?
>> That's exactly right. And what we've got is an object, which is probably wrong.

[00:02:43] So let's look at that. And let's just see if I got my implementation wrong of either or if it's just not looking right. Let's do fold. Hey, let's bring in that fun identity function to fold it out right? We'll call it ID. We'll do x and x, cool.

[00:03:04] Fold ID, ID. All right, let's see if this one works. Okay, this one works. I just messed up my identity function. Actually, this will be kind of fun to see how this is implemented. Let's go over to our types. Now, let's find identity in this chaos. And we're saying, we hold something, x, look at that.

[00:03:27] I didn't program to the interface. [LAUGH] That's why, here we go, cool. So as we can see here, we have an ID concating its innards with another one, right? So what it's doing is saying whatever I'm holding, have that concat with the other one, and extract that out to get its x.

[00:03:49] And then we'll go ahead and keep it inside the ID because it's closed under composition. So let's put it back into ID, ID and then we should be able to extract out of here. There we go, cool. So that's the benefit of programming through an interface if you actually do it.

[00:04:12] So, one other thing we can do here is, so these stack as much as we want. Right, we can put an ID of a right of a task [LAUGH]. And it just composes. It just straight up will cascade, concating whatever this is holding with whatever this is holding.

[00:04:38] The point is, as long as I'm concating something with the same shape, if we go back to that gobstopper example, we took all these different effects and we made it into the same shape. It's kind of this layered type. And I can actually concat that with another thing.

[00:04:54] And it will open up the ID, open up the right, open up the task and concat the sun together.