Transcript from the "Defining Empty Identity" Lesson

[00:00:00]

>> Now that we've defined a few of these, I'm gonna go a little further and explain how this works as a monoid. And then we'll see how it applies to functors. And then we'll do some exercises and build a little library on them. But monoids are just a semigroup, with an identity, so a monoid is a semigroup, monoid, semigroup, plus an identity, okay?

[00:00:35] And you asked about the identity function. Here's an identity function. We're going to call this one empty in the interface, but it's just an identity function. So products, if I call it empty will be one. And the reason for that is if I multiply anything plus one I get that thing back, right?

[00:00:55] I have to put it in a product. There we go. Let's see how that works down here. So if I have my products, concat products 10, I'm gonna get product 10 out. It's just an identity function. And so instead of being specialized to 1, I can say product.empty.

[00:01:22] Now, I can concat that with 10 and I am programming to an interface, not a specific value. Let's do the same thing for sum. What's the identity for sum? I'll say sums.empty function is sum of zero, right? So that'll be the identity. If I concat that with any other sum, I get zero.

[00:01:43] Why does this matter? Does anybody have any guesses on why the identity matters here?

>> Null values.

>> That's exactly right. There's some cool stuff with Kaylee's theorem where you can partially apply everything with his identity and get these cool lazy functions and whatnot. But mainly In our day to day, if I was to run through a list and reduce it down, if I don't have a starting value, I'm gonna get a null or an undefined.

[00:02:11] But if I have a starting value, these these two correspond to reduce. So let's take a look at that. So we have a list of 1, 2, 3, 4, 5. And we reduce that. And we get the accumulator and each number. And let's just go ahead and say I will put the number in a sum.

[00:02:34] And I'm going to concat that with, I'll start with the accumulator concat number there. Okay, great. So what's gonna happen here is hopefully, we'll get the sum of all these numbers. And let's run that. All right, we won't. [LAUGH] See at.concat is not a function. All right, I guess it doesn't start.

[00:02:58]

>> You gottta put sum as the secondary argument and reduce. Yeah, it's not gonna work. Well, let's do it. Let's put them all over here. Let's just map them into a sum. And then we'll reduce them all. That makes a lot of sense. Okay, so now we have a bunch of sums.

[00:03:15] Everybody's happy. But we'll see here that we're not giving reduce its empty value. We're just giving it a list. So a list of sums in this case. And we get 15. But if we give it an empty list, what's gonna happen? It's just gonna blow up. And the reason for that is reduce of an empty array with no initial value.

[00:03:42] So we have nothing to return. If we give it a list of nothing, we have nothing in return. But if we give it a list, we start with sum.empty. There we go. We can actually get 0 back. So we have a way to proceed in our program with an initial value that says okay, even though we didn't have any data to process right now, we can still provide you some kind of value.

[00:04:07] And you can continue on. So just blowing up being like here's a null. So, one thing we can do here, this actually happens quite a bit is that we want to put stuff into a monoid. And then call concat. And start with empty. This whole thing is almost redundant.

[00:04:28] This is actually how I would sum up a whole list, right? Here's how I would get the products of a whole list. I just put it all into a product. And then I'd run through a list and get the product of them. What if I have a bunch of true, false, true?

[00:04:42] Let me break this onto the next line so you can see what's going on. But all right, let's go ahead and put those into an any and say hey does any have an empty? Let's see. What would act as identity for any? If I said any true concat any false, that would return any true.

[00:05:06] All right, so that acts kind of like an identity on the right side, any false. If I said any false concat any true that would be any true on the right side. Okay, so what about any false any false?

>> That would be false.

>> That would be false.

[00:05:23] Okay, so therefore, if this was empty I would get any false, false. Yep, true and false. Sorry, I'm just working this truth tables out in my head. So let's try that. I would say any.empty is a function plus any false. So if you hit any trues along the way, it'll return it back.

[00:05:52] But this acts as an identity. If I give it any true and false again any true back. If I give it any false false get any false back, identity. Cool, so we have that. We can alternatively do all with the and. Create, and then this will be all.

[00:06:13] Let's go ahead and define an empty for all. And that would just be opposite., all true. And if anything is false, it will just flip the switch and be false. So now we can reduce this, say are any of these true? And this is the same exact template again and again and again, right?

[00:06:31] If we run this, we get true because any of them are true. How about all of them? Are all of them true? That's not true. That's false. There's a false in there. We get rid of it. Then they will all be true. Great.