This lesson continues on the topic of monoids by teaching viewers how to create a generic fold
function that consumes a monoid as well as an array which that monoid will perform a reduction on.
By the end of the lesson, you'll have created a helpful utility function for creating other specialized functions that are enclosed with a particular monoid.
Thomas Greco: [0:01] On our page, we have a collection of several different monoids. At the bottom, we see that we're logging out our Product monoid, after concatenating it with several different values, and we're doing the same with our Sum. Although this will provide us with the values that we need, it's not exactly the most practical way.
[0:25] Let's see another way how we could do this. Here, I'm just going to log out result, which I'll make right here. Above that, I'm going to create a data variable, and it's going to be just an array, 1, 2, 3, 4, 5.
[0:55] To use our monoids, we're going to want to map over our data. If we pass the sum monoid, we should see that this returns all these values wrapped inside of that sum monoid. [inaudible] see we have that concat right on it, and we'll do the same with product.
[1:33] Once we have that, we can then call the reduce method, and we're going to say the accumulator concat with the val. For Product, we'll have to pass in Product that empty here. If we take a look at our console, we see that all of our Product monoids have been reduced into this single object which has the value of multiplying one times, two times, three times, four times, five.
[2:19] If we switch this out for Sum, we see that we get 15 there. This is more practical than actually manually calling the concat method every time. However, it's still not that general as we're forced to define the exact monoid that we want to use. Let's make this a little bit more flexible.
[3:01] Here, we're just going to create a fold function. It's going to take in a monoid as well as some data. Should default to an empty array. Then it's going to reduce over this data, just like we did up here.
[3:28] We could just copy this. Instead of sum.empty, we change this to monoid. Instead of concating the vowel like this, we're going to wrap our monoid around the value.
[3:50] At the end there, we see that the initial value will just be our monoid's empty function Now, we'll say result equals empty sum and then data. See, we're getting the exact same value as we were before. The same will go on with product. If we wanted to, we could just say guns sum fold equals fold, and then pass in sum there. Then sum fold data.
[5:02] To wrap things up, let's say list of Bools. This is going to be a list of Boolean values. We're going to have all true there. We can say fold, and then we're going to pass in our All monoid, and then listOfBools. Boom, we see that's printing out true for us. If we change this to false, we see that's changing to false. Then we do the Any type, just to check everything out.
[6:15] We'll do the Max. We see that we're getting the Max value here in 5. If we get Min, we see that we're getting 1.