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.
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.