2:50
JavaScript
lesson by Brian Lonsdorf

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

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.

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.

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.

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.

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

Wait. I thought fold took a function.

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.

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.

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.

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.

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.

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.

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.

## Comment Guidelines

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

## Commenting Guidelines

Here are some basic guidelines for 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 click here.

## Avoid meta-discussion

Is your comment resembles:

It will likely be deleted as "meta".

## Code problem?

Should be accompanied by code! JSFiddle, Plunker, CodePen, etc all 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!

egghead.io comment guidelines

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