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 "Category Theory" 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 category theory, you need a composition and an identity to form a category. Brian introduces category theory and defines various category laws like left identity, right identity, and associativity.

Get Unlimited Access Now

Transcript from the "Category Theory" Lesson

>> Brian Lonsdorf: The voyage [LAUGH] so I love how big it sounds [LAUGH] okay, I'm gonna start-
>> Speaker 2: I can fall in love with that.
>> Brian Lonsdorf: [LAUGH] The voyage. So the point, the point is that If I add one to one, I get another number. That's two. And everybody should know exactly what that does even though you don't know what the implementation is, because it's bigger than what I came up with.

[00:00:31] Somebody else came up with it a long time ago. And it's got some properties and laws with it. So we know it's associative. That means it doesn't matter how it's grouped. It's always just gonna end up with the same result. It's commutative. We've got identity and down at the bottom, we even know how it works with other functions.

[00:00:52] This is incredible. Could you imagine if all of your functions had this book of, you can do this with it? And you can always guarantee, we've got these theorems and formulas. You're basically, you don't have to know it. You can just add without that. But, if you want to, boom, an ocean of knowledge, right?

[00:01:13] So that's pretty beneficial. And with other, another cool thing about add, I'm basically regurgitating my fluent talk right now, but I'll get in other stuff. This, right here is, you've got an intuition behind it. And it's polymorphic. So we can add strings together, we can add floats together.

[00:01:34] I mean, floats are still numbers but in other languages it still works, big nums, whatever. Down at the bottom we're adding arrays together. There's an intuition behind add, and those laws will still hold. Maybe not for this particular crazy add. But when you work with these other functions that have these laws with them, they'll always hold for every type.

[00:01:54] Which is really cool. So you have an intuition and laws attached. Hey, guess what compose is? [LAUGH] It's like add. It's got laws and it's got properties, and it's actually polymorphic. You can use it on other things besides just functions. It's incredible. It's just like add. So we're gonna go into category theory.

[00:02:19] Probably weeping at this point, like, what I did sign up for? But if nobody understands this type signature, cuz I just threw it up on the screen. The (b -> c) is a function that takes any variable, any type b to any type c. Second one's any function that takes a to b or returning a function a to c.

[00:02:38] So basically it runs the middle one, and then the first one, and ends up with the last one. But you guys know what compose does and we'll see a lot more type signatures, it's not real important.
>> Speaker 3: Is that a Haskell?
>> Brian Lonsdorf: That's a Haskell.
>> Speaker 3: [CROSSTALK] That's a Haskell set.

>> Brian Lonsdorf: That's a Henry Milner, yeah. There's no big deal with it. This is kind of a complicated one. I'll walk through them as we see them. And you don't really need to know this one, but the important part about category theory is that you need a composition and an identity to form a category.

[00:03:12] And we're working with the function category when we're dealing with just normal compose and identity on functions. Does everybody know what the identity function is? Basically you give it a value and it just spits it right back out at you. [LAUGH] So you're like, why would I need that?

[00:03:28] It's pretty valuable when you're doing point-free programming, you could do filter ID to refer to the thing going in. It's kind of weird, so, but the point is you need an identity function and a composition function and you can have the categories. So, it's kind of a design pattern we're gonna use in our programs.

[00:03:45] So here, there's laws associated with it. That means if I compose the left and right identity, it's always just gonna be just like running the normal function. Cuz if identity just spits the argument right back out at you, I mean what's the point? It's always just gonna work.

[00:04:01] Down at the bottom is associative. That's why we've been able to compose x amount of arguments. It doesn't matter how they're grouped. It's also why we're able to just grab any subcomposition any two functions in a row in our compose, and pull it out into its own function.

[00:04:15] So this isn't Haskell syntax, no. Whoa, my mouse is all crazy. That's weird, it's like a delay, okay. So like we saw over in this over here, we could grab out any two. If I had F and G and H right here, I could just be like, these go together.

[00:04:38] Boom, and I'll make a new one called J that is the subcomposition of these. And the reason for that is that compose is associated so it doesn't matter how these are grouped together, so you can pull them out in their own functions. So that's really neat. It's just like add.

[00:04:57] It doesn't matter if you add 1 and 2 or 2 and 3. They always end up in the same result.