Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course

The "Composing Functors" 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 explores different types of functors using the map, extract and fork methods, and explains that functors have different identities, can be composed together, and have a category within which they act like functions.

Preview
Close

Transcript from the "Composing Functors" Lesson

[00:00:00]
>> I wanna start off talking about function or Mona Transformers by talking about functor composition. So what we have here is a type that says compose. I'm gonna take two other types F and G and I'm gonna return you a new type M. And if I wanna put something in M, we'll put it in the G, in the F, and in the M.

[00:00:26]
And if I wanna map, I'll map over the g, and map over the f, and keep that in the M. Cool, all right, let's see how this works. I can say, well, I have let's call a TaskEither. And I'll make a compose. And I'll Compose(Task, Either), okay? I wanna put something in a TaskEither.

[00:00:47]
So it's a task on the outside either on the inside. That's right. So then we'll say TaskEither of, to. Give myself some space will map over that we have a to two times, 10 will map over it again just for funsies two or this 10 20. [LAUGH] Goodness.

[00:01:15]
So we got twenty <> twenty + 1. All right, and then we're going to fork this, because it is not a task either. It's a compose of TaskEither. So we actually have to get it out of the the compose. Now I've shown extract a few times. And for those of you who new fold and such, extract is coming from, it does not take the function and then pass that to the function.

[00:01:42]
You just get it out and it's part of the common ad interface, but it's only one piece of that. Okay, so we can extract out of the compose to get our TaskEither. And once we call this console.error and then we get the either in there, then we could fold out the either to a console.log, console.log.

[00:02:12]
All right, terrific. People see how this is working. I have a task of either of mapping over the value inside of it and to take out of the compose. So I get my task, and then I run that, and the task is holding either, so then I get my either.

[00:02:30]
But during my whole computation, I didn't really have to care, I didn't have to map map all over the place. My one map just did both maps for me. So if I run it transformers/0, we got 21, perfect. And so functors compose effortlessly. Any functor just composes with others.

[00:02:52]
In fact, you can compose it with compose, I could say I could compose identity with that and so on, and it's associative. And we actually have an identity functor don't we? So actually functors themselves for form category where they act like functions. You can compose them and you have an identity and then they provide this whole kind of one click up from functions.

[00:03:14]
It's a whole composition toolkit. But we're not gonna go too far into it, where things get hairy. If I do chain I have to somehow provide a TaskEither. So I get into this too and I have to deal with creating another task either to chain that. And that is not mechanical.

[00:03:39]
That doesn't always work. You can't just compose two mon ads. And just expect it to chain for us. We can't write a chain that makes sense here, in compose. Does everybody understand that fact, maybe not why but at least there's a fact that says we cannot write a flat map or a chain in here.

[00:04:00]
And we make sense like for each montaic, kind of each type has its own way to specify map and chain if you're gonna fall out of the type. You have to know about the details and this you just it just bottles it right back up for you so you can always map map and you're fine.

[00:04:20]
So we cannot chain these these things. So what are we gonna do? We saw we could, we can kind of combine task and either into a monoid and just say like, I'm just gonna keep combining my map my task eaters and work with the stuff inside it through concatenation.

[00:04:39]
But typically you're going to want to map and chain, so you could run a composition of functions over it instead of just combining their insights.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now