Testing Semigroups to Ensure They Abide by the Law of Associativity

Thomas Greco
InstructorThomas Greco
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Semigroup Data Types provide users with a .concat function which must abide by the associative law. As a result of that, it doesn't matter how we append .concat as it will always produce the same result.

Fantasyland Docs - Semigroup

Thomas Greco: [0:06] Below our Sum Semigroup, I'm going to create a Product Semigroup. We can pretty much just copy the details and paste them below. However, we'll need to return a product from our concat. We'll also need to change our addition to perform multiplication.

[0:31] Rather than logging this out, I'm going to create a unit test. We'll say our Product Semigroup, async assert.

[0:53] Then we're going to say Product(10). We're going to say given: ".concat(Product(5)).concat(Product(2))" should: "return a value of 100". Here, I'm going to say result = Product(10).concat(Product(5)).concat(Product(2)). We're going to say result, and the expected is going to be 100.

[1:57] We need to peel off the value. We can use our ramda function. We'll say equals(result.value, 100). Then we'll just write true here. Now we see that our test is passing.

[2:24] Below this, we're going to test the associative nature of Semigroups. In order for us to be working with a Semigroup, it must be associative. That simply means that we can perform these concatenations any way that we'd like.

[2:52] Here, we'll just do const first =...We're going to set it equal to this one right here. Then our second, instead of having it just like this, the value of 10 * 5, we're going to concat directly onto the product of 5. We'll do equals(first.value, second.value), regardless of where the concatenation takes place. Let's just see this run. Boom, we see that our new test case is passing.

[4:19] Although it might seem like a minor detail, it's extremely important that our Semigroup possesses this attribute of being associative, or else we won't be able to harness the true power of Semigroups.

egghead
egghead

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