Autoplay

    Unbox types with foldMap

    Brian LonsdorfBrian Lonsdorf

    We get a clearer definition of what it means to fold a type, then we look at the foldMap function

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:01 Now here we have a list of sums, and we want to concat all of these together. We just reduce it down, and concat each one together, starting with the empty value.

    00:09 Now this is a very common operation. We can actually rely on the fact that since there's nothing very special about this function here, it's just calling concat, we could just call it fold sum.empty here, and that would do the exact same thing.

    00:21 We've defined fold knows how to concat all the things together, and then we give it the empty value to start it off. The reason we have to pass this in is because there's no way to know if it's an empty list, where to start, and what to return. We have to give it that.

    00:33 That's not the case in a typed language, but here in JavaScript, yes, it is. We don't have a fold on the array here, though. No, we have to use a list type here, which has defined fold just like this reduce down here.

    00:44 Now if we go look at this, we shall get a nice sum of the list. Very good.

    00:49 Wait. I thought fold took a function.

    00:51 Ah, yes. Fold is a fairly overloaded term. However, it always holds with the same intuition. If we remember box of whatever we have there, if we fold it down, it will just remove it from the box. It's just like map, but it will drop down a level.

    01:07 We did the same, where we have a right and a left. We have two handlers here, the error case and success case, but it still removes it from the type. Now what about lists, though? We have numerous values. We have a collection of things. We need to remove it, but we want to just take one thing out, as such, with the fold. We want to be able to summarize the list, as it were.

    01:26 Here, with the fold, it is the same intuition, we are just relying on the monoid to be inside the collection so that we can extract one value, in this same sum six. Whenever you see a fold, think removal from a type, be it a collection which relies on a monoid or just a single value in a type.

    01:42 Let's go ahead and look at a map here. Let's do the same thing. We'll say Brian is sum two, three, and Sarah is sum five. We can do the same thing with a map. It's a collection of things, and it will just sum the values in the type, extracting it out.

    02:00 With a map here, we don't normally have a monoid in the value slots, or same with lists, we don't typically walk around with lists of sums. How might we put these values into monoids? We can just map over it, and just put each one in a sum here.

    02:18 If I can just do this first class here. Sum, there we go. If we run this, we get the sum of eight still. If we had a list of one, two, three, and we just map sum over those, it will add it together. This mapping, then folding, this put everything into a monoid for us, and then fold it down, is so common, we have a function called foldMap.

    02:36 This will just take the function to run on each, and then our empty starting value as a second argument here. It's the same as first map, then fold. We have foldMap. Pretty intuitive. If we run this, it should be the sum of six still. Very good.

    Discuss

    Discuss