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

The "What is a Semigroup" 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 explains that a semigroup is a structure that contains an associative operation, and that is closed. Explanation is given for why a closed and associative.


Transcript from the "What is a Semigroup" Lesson

>> So we're gonna start here with monoids. And I'm gonna define what we call a semi group. Raise your hand if you've heard of a semi group or used it. Okay, we got one. All right, how about monoids, people are using, cool. So, we're gonna start by defining a semi group and let's look at some examples.

So one plus two, plus three plus six, doesn't really matter. What's happening here is it doesn't matter if I parenthesize these ones in the right or the left first, I'll get the same answer. So again, associativity keeps coming into play. So addition is associative, it also is closed.

So if I add a bunch of integers I can never break out of integers with addition. I'll always get integers back, so it's closed under composition. I mean this is the composition so binary operators, we're gonna compose these numbers. So now, let's go ahead and say, we’re gonna take the product of a few numbers.

So how about two and five and eight. And again, it’s associative right? It doesn’t matter how I parenthesize this operation. And again, it is closed. Under the data type I'm getting the product of. Now, contrast just as a counter example, if, let's say I have 10 divided by 4 divided by 2.

This is neither closed nor associative. So if I do four over two, I'm gonna get two, right? And then 10 divided by two is five. So there's that answer but if I parenthesized it this way 10 over 4 is like 2.5. And that not over two is like, 1.2.

And it's, so we're breaking out of integers into floats and we're getting different answers but depending on how we parenthesize. Just as FYI, if you're closed and you're associative, you're parallel. That's pretty neat. You can parallelize that, you can chunk it up into a bunch of different additions, right?

x plus y plus z. We can send it to different servers have them all combined and then combine the results. And we can do it in parallel. That's pretty great. Spelled associative wrong, but whatever. Let's look at more examples of this. Let's say we have a true and a true and a false.

This is again associative. We could do ORS, that's associated with we're just looking at these binary operators they are associative and they are closed. There's something to this, how about set intersection, we have 1, 2, 3. And we'll do the meet, that's not a real operator. I'm just making that up for this.

We have 2, 5, 6 and do that again, we could, essentially that's associative, we'll get the intersection of all the arrays, right? So in this case, it'd be two but, let's go down to this junction or union. You get the same thing. You get the union of all them.

This is associative, it's closed. So try to think of any other examples of a semigroup, that's that's what we're getting at is any kind of-
>> What does closed mean again?
>> Close means that we're, if I'm going to get the intersection or union of these sets, I'll get a new set back or array in this case.

Same thing with integers. I don't change the data type. When we looked at division, I know in JavaScript is just number. But we had integers and we were dividing integers and boom, you get a float or you get a real number out of nowhere, right and so that is not closed.

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