This course has been updated! We now recommend you take the The Good Parts of JavaScript and the Web course.

Check out a free preview of the full JavaScript the Good Parts course:
The "The Identity Monad" Lesson is part of the full, JavaScript the Good Parts course featured in this preview video. Here's what you'd learn in this lesson:

While some experts believe in order to understand monads you need to first learn Haskell and Category Theory, all you really need is a solid understanding of JavaScript. A monad is an object. A monad example: The identity monad.

Get Unlimited Access Now

Transcript from the "The Identity Monad" Lesson

>> [MUSIC]

>> Douglas Crockford: So they will tell you that in order to understand monads, you must first learn Haskell. It's that's where you start, and you have to learn category theory, without those monads are meaningless. I think that's like saying in order to understand burritos you must first learn Spanish.
>> Audience: [LAUGH]

>> Douglas Crockford: And it turns out that's just not true. Without knowing any Spanish you can order a burrito, you can eat the burrito, you can enjoy the burrito. You can even make your own burritos, you can invent new kinds of burritos. You can do all of that without learning Spanish.

[00:00:42] Now I'm not saying you shouldn't learn Spanish, there are really good reasons to learn Spanish. If you really wanna get deep into Mexican cuisine, if you wanna make more authentic burritos or other things which are even better learning Spanish certainly helps. Learning Spanish allows you to have relationships with people you would never meet otherwise.

[00:01:02] If you're one of the job creators, it makes it possible to talk directly to the people who are actually doing your work for you. There are lots of good reasons to learn Spanish, and similarly there are good reasons to learn Haskell. Haskell is an amazing language, there is much it can teach you, but you don't need to learn Haskell in order to understand monads.

[00:01:21] So they would say, no, that's not true. Before you can understand monads, you have to understand that. That's where it all begins, and unless you understand that, it's all meaningless. And I'm saying, no, that's not true. If you have the juevos, you don't need this, you can get past this.

[00:01:42] They will tell you that you need the type system that is build into Haskell in order to manage the complexity of monads. Without that type safety it's impossible, and I'm telling you that's not true. If you have the chicharrones you will find that the type system is actually making things harder, not easier.

[00:02:01] So the place we need to begin is first we have to learn JavaScript, cuz it turns out JavaScript is the perfect language for expressing monads. And here it is, okay, we've got a unit function that takes a value, we have a bind function that takes a monad and a function that takes a value.

[00:02:22] So three functions, they all return monads, that's it. Now okay, so what's a monad? A monad is an object. Okay so, we've got unit, it's a function that takes a value and returns an object. If you know anything about JavaScript you might say that sounds like a constructor.

[00:02:40] Is unit a constructor? Yes, you're absolutely right, unit is a constructor. So we already know about constructors, so if there's any magic here, it has to be in the bind function, right? So yeah, that's where the magic is, and it turns out there's not much magic here. So, of course, there's more to it than that, the monad axioms.

[00:03:03] The first two axioms describe the relationships between bid and unit. And the third one, which is the really powerful one is the one that describes composition. And composition turns out to be what what this all about. It's a way of taking functions and putting them together in colorful ways, which is what we've been learning about it today.

[00:03:29] So the monads are usually described in the function form, but I think it would be more convenient to methodize them and turn them into a method form, because it's easier. And we know how to do this, this is a trivial transformation. This is the object already transformed, right?

[00:03:47] And we do this in JavaScript all the time and we can be doing stuff either in method space or in function space and we generally choose the one which is most convenient and that's how we do it, right? So nothing mysterious there. So this is our monad thing.

[00:04:07] We'll talk about what this is in a moment. First, something about the colorization. So you noticed I've got this coloring scheme here, and I've been using this all day. So you are all familiar with something called syntax coloring? Anybody heard of that? It's something that we have in IDEs and text editors to make it easier for kindergarteners to write programs.

[00:04:31] It uses bright happy colors to say that's a string and that's an operator just so that everybody knows what it is. I don't see much value in syntax coloring, cuz I'm more sort of a grown up. And I don't have any trouble figuring out what's a string and what's an operator and what's a variable name and so on.

[00:04:52] What I want is context coloring, because I have lots of nested functions. And I want to know immediately where something was defined, because that turns out to be important. So here I'm using context coloring. So white means the global scope, green is all functions that are defined at the top level.

[00:05:16] Functions defined inside of those are yellow, functions inside of those are blue. And within those functions, any variables that are closed over outer functions have the color that they were defined with. In that way I can see where all the functions are and where their variables came from and it makes it much more easier for me to recognize what's actually going on in the function.

[00:05:40] And I wish someone would write me a text editor that's got this kind of coloring in it, cuz the other coloring doesn't help me at all. And this would be great. Now another thing that happens in monads is, in some languages, like in Common Lisp where they have powerful macro systems.

[00:05:58] Someone will create a macro library for creating monads, because most monads are structurally very similar. And then you use those macros and just specify how your monad is different than the standard monad and you're done. JavaScript unfortunately doesn't have back rows, at least not yet. But it does have functions and it does have objects and with those it's possible to get really close.

[00:06:26] So this thing here is a macroid, it's a macro-like thing which we will use for making other functions. And we kind of been playing with those the all afternoon. So let me walk through this macroid, it's going to return the unit function, okay? And the unit function will make objects that contain a bind method, okay?

[00:06:54] So that's pretty much all we have here, right? And the bind method, takes a function and receives the value that was used to initialized the unit, and passes that value to the function, and that is it. EK mono behold the monad. Yeah, that's it, that's really all it is.

[00:07:20] So this is how you would call it, we would make our unit by calling MONAD. We'll pass the unit, Hello world. We'll say, monad.bind(alert) [SOUND], Hello world. That is it, that is the MONAD. Not that complicated, is it? So this particular monad is called the identity monad. All it does is take a value and give the value back to bind, and that's it.

[00:07:48] And all other monads grow from this one, and add additional complexity, but this is where it starts.
>> Douglas Crockford: And these are the axioms, it's kind of amazing that this really is all it is. But you got to read those tutorials, and you'll get no sense that this is all this is.

[00:08:09] They get really complicated, and so you wonder why these guys go around saying monad in mixed company without actually telling anybody what it is. That it's really this basic thing. So these are the axioms rewritten in our method notation, okay? So I think the first two actually start to become a little bit clearer, but the really interesting one is the third one.

[00:08:36] The composition axiom, particularly when you compare it to what it was before. It used to be that we were having to write in this inside-out pattern, right? Whereas now we can just string things along and the composition becomes much easier. In fact, if any of you have done any amount of Ajax programming, this might start to look kind of familiar.

[00:09:03] We've got an object.method.method, where have you seen that before? You've seen it everywhere, every place on the web. This is how we do it, this is the Ajax monad, right? We've been doing this stuff all along and we didn't know it.