Transcript from the "Creating Semigroup Data Types" Lesson
>> So semi groups are everywhere, like, everywhere. And so the main thing you have to kind of think about when you're defining a semi group is what if we make this operator this kind of like special operator here and we're going to overload it with plus union intersection, disjunction, conjunction and pretty much any operator.
[00:00:22] We can do max and min in the same way with min greater than and less than. So if I said one greater than three greater than seven or zero, whatever, we can do that associatively and it's closed. So semigroups are everywhere. And we're going to define them in programming as a datatype.
[00:01:00] So if I'm gonna concat a sum with another sum, well let's actually start with what we gonna write so we'll say 3 concat sum of 5, and what we want is the sum eight. Now that's a weird thing to do. [LAUGH] What we're trying to do is make an interface that we're programming to that captures all those examples that I was just talking about with the semi groups.
[00:01:26] So we have these binary operators that can be implemented in many different ways and they're associative and closed. We can make an interface on an object and say around a type and just say concat is that operator, right? It concats the thing on the left with the right and it's associative and it's closed, meaning I will always get a sum out if I concat two sums.
[00:01:50] So let's go ahead and implement this. Sum we're always going to return a sum and we have to expose this x here. So I can get the x off the other. So we'll say sum x plus other.x, so everybody see how that works. That's a it's a bit of an interesting syntax.
[00:02:11] You don't see it very often. With these little factory functions, I just, it's the slimmest way to define this kind of stuff and I just stick with it. But is anybody getting hung up on this? Okay, so we're going to do is hopefully this will say sum of eight, I think monoids zero.
[00:02:36] It's not really going to say sum of eight, but x is eight, and it's got a concat. So that's our sum. We can define a two string on it. That would be nice, but let's not go through that. So we can do this with products. Let's go ahead and do this with products.
[00:02:55] And that would be just multiplication, right? Instead of plus, cool. So let's change sum to product. Three times five and we should get the products. There we go. Oops products. Sum five. What did I do here? There we go. And there we are. 15 how about let's do any, which is the name for this or that.
[00:03:30] So let's say any is x for the other one. I could say any true concat any false. Okay, and so if either of these are true, we'll get an any true and if either, so we can just run that real quick. Get true. They're both false, we'll get false.
[00:03:55] This kind of captures that idea of running through an array and seeing if anything is true or false. It combines everything and tells you the answer of if you had a true or false, so we can see we're going to see some of these in action. But I just want you to get a feel for what's happening.
[00:04:13] We're lifting this operation into a type so we can program to the interface called concat. And then we can swap these types out. But concat always behaves the same way. Perhaps the interesting the simplest one is just strings. Say hi [LAUGH] concat it's already built in got an explanation.
[00:04:35] And bam. We get hi.