Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course

The "foldMap" Lesson is part of the full, Hardcore Functional Architecture Patterns in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that foldMap takes elements, maps them into a certain type, and then folds them.

Preview
Close

Transcript from the "foldMap" Lesson

[00:00:00]
>> Okay, so we've defined a template for taking a list of things and combining them. We're going to call that function fold, actually fold map. We're gonna take this, map it into a type and then fold it down via concat. So I don't wanna rate fold map here.

[00:00:24]
I'm just gonna bring it in from a library. So we'll call this const. I mean, we essentially just did right fold map, we'll just have to pass in this type and that type. But I'm just gonna go ahead and say list. We're gonna require immutable-ext. Immutable-ext. Cool. And that defines full map on the list, because we can't call full map on an array, right?

[00:00:52]
So I'm just using the list to be able to call full map. And now I don't need any of this. [LAUGH] I'm just gonna say put it into all, full map it. Great. And it's probably helpful to provide all that empty because then we'll have a starting value.

[00:01:12]
And we'll call it toJS because it's immutable. Good times. And run this. Again, it's not findking immutable, toJS is not a function. Because it's folding it out of the list. That's kind of interesting. It's consuming it, right? It's turning it into an all, so we reduced the list into an all so we don't actually have to call toJS.

[00:01:37]
Cool. Still works.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now