Leveraging Semigroups to Create Reusable Any & All Interfaces

Thomas Greco
InstructorThomas Greco
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

This lesson extends upon our knowledge of semigroups and shows viewers how to create Any and All semigroups which provide us with an interface for comparing boolean values.

Fantasyland Docs - Semigroup

Thomas Greco: [0:01] On this page, I'm just going to create an Any and an All semigroup. I'm just going to scaffold out the shell for both.

[0:14] Inside of our Any, the first thing we're going to do is set the value, as always. Then below this, we're just going to add a concat function which is going to use the || operator to check for truthiness in either the current value as well as the value that it's being concatted with.

[0:38] Below, we're going to do the same for our All semigroup. However, this time we're going to use the logical && operator as we're going to need truthiness in both of these values in order for this to return true.

[0:58] Now that we have those set up, I'm just going to copy our minTest and maxTest variables, paste them down here. Instead, I'm going to change their names to allTest and anyTest.

[1:14] Inside of our allTest, we're going to change all of our Maxes to All. We're going to pass in false into our first All, and then false to our second All as well. Last thing we're going to do, let's just pass in a true. For anyTest, copy this line down here, and then our Alls to Any.

[1:47] Now if we run this...Oh, we need to log them out. If we run this, we see that we get a false for our allTest and a true for our anyTest.

[2:10] As we know, our allTest is going to need truthiness in all of our values. Therefore, we'll only get a true if we set all of these to true. Now if we run this again, boom, we see that we're getting true for both.

[2:27] If we change our last Any to take the value of false, we'll see that this now prints out false as we have no truthiness in any of these. If we set all these values to true, obviously we're going to get a true back.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today