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

The "Semigroup vs Monoid" 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 the main differences between a monoid and a semigroup. A semigroup can have an empty identity.

Preview
Close

Transcript from the "Semigroup vs Monoid" Lesson

[00:00:00]
>> Or we can do some other things, we can do max and min, and intersection and union. I wanna provide a counter example to monoids and show that we can't promote a semi group to a monoid with an identity. So one example of that would be interest section.

[00:00:15]
So if I make intersection and I try to, let's just copy my little template here. And I try to say okay, well, I have the intersection of this and that one, we'll use like low dash intersection, right, of this and the other one. And now how would I provide a value that said, a starting value to get the intersection of the list, right?

[00:00:47]
If I concated that, if I had an intersection of this list, and I wanted to concat that with the intersection of another list. Blah, blah, blah, blah, blah. What would happen is if I provided an empty list, nothing would ever intersect with it. I would always get an empty list back.

[00:01:09]
If I provided a list of every possible anything, that might work. [LAUGH] But I don't know how to do that. So we're just gonna say that intersection does not have an empty. Therefore it is only a semigroup, it is not a monoid. And so that's interestingly a union, we could just give it an empty list and that'll be identity.

[00:01:33]
So, union is a monoid, intersection is not.

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