Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "The Endo 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 explain that the endofunctor is called endo because it only works with the same kind of types.

Get Unlimited Access Now

Transcript from the "The Endo Functor" Lesson

>> Let's do a different type of function. Get rid of this stuff and focus on, what if we would rather instead of combining functions by running both and then combining the results, which is kind of the normal intuition behind concatenating factors. We can do is make a composition like function composition as concatenation.

[00:00:23] So I have like a thing where I want to go to upper and exclaim, right? And I just wanna like fold map that and I want to combine. I want it to do both of those things. So let's do that. And for that, we're gonna make a type called Endo.

[00:00:42] That was if anybody saw my Snoop Dogg slide earlier, but [LAUGH] we have Endo.empty. All right, so what would that do? That would provide me a function that when I run it with my string hello, I should get HELLO all capital. It's got a first to operate and then explain.

[00:01:05] It;s just gonna run through a pipeline of functions and new composition. Now, the reason it's called Endo which is weird is because this only works with endomorphisms meaning it only works from types a to a, in this case string to string or task to task or whatever to whatever it has this have the same type of input as output.

[00:01:30] And the reason for that is because now we know we can just compose them without running into any type problems where they don't compose [LAUGH]. So this just kinda skirts that and says, okay, if they're always the type, same type of input is output, we'll just thread it through and they'll always compose.

[00:01:45] Let's write Endo. Doo doo doo doo doo doo. And if we have time we'll talk about one more. But this should be fine for now. So we have Endo, let's not worry about mapping chain and anything. In fact, you can't map Endo, endo is a function from string to string or a day or into int or whatever it's always the same type.

[00:02:15] If I tried to change the second type with map if I returned my value in the map over it then it would break everything I would have it would not be Endo anymore. So it is not a functor. I do have to talk about one thing after this.

[00:02:30] Don't let me forget. [LAUGH] We're talking about contra map and contravariant functors. So, we don't have map on Endo, but we can say, just like the other ones we'll say with Endo and x instead of running both and concatenate the results. I will run the other one and then run mine with the result of the other one.

[00:02:54] Cool. We'll have to throw this in a little list Wow. [LAUGH] .List list from immutable j s are extensions and we'll foldMap down Endo, we don't have Endo.empty, what would Endo.empty look like? We have an Endo, what's next? X be Id, does that work? So what happens? [SOUND] Op, and I'm gonna show you the result, but also it should say Endo, it should have a string in it, right?

[00:03:31] Do you do that? There we go. That looks better. [LAUGH] All right, so we have a result. And we'll fold map this thing down. And then we'll run it. Yeah, right down here. And we'll console that log. The result. Don't know how to do this efficiently and we get nothing.

[00:04:00] Goodness, that's not good. What's happening here? So the original empty, I'm suspecting is our problem. Let's go ahead and focus on that. So it takes an empty string original Endo takes an empty string. And then when we run it on this thing, I think it ignores its argument right here and just returns an empty string.

[00:04:26] So perhaps, that was wrong and we'll say empty takes this and we'll see what happens there, okay. That works. I have to clarify this one and come back to you because I'm getting confused, but at least it's working. So the point here is that we're combining all these by composition.

[00:04:47] And it kind of doesn't matter that we're running the other one inside ours are ours inside the other. Like if we did it this way. The order kinda doesn't matter. But then we go back to our empty method, right? Where we do that I'm trying to make of that's why empty takes no arguments.

[00:05:09] That makes a lot of sense. There we go. So that clears that up.