Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course
The "Flattening Either Monads with Chain" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:
Brian demonstrates how to flatten the Either monad using both the chain and map methods.
Transcript from the "Flattening Either Monads with Chain" Lesson
[00:00:00]
>> We're gonna do here is, is we're gonna switch this map to a chain, and we're gonna return another Either in here. So that's the flag. If we didn't do map here, we're gonna put in another tryCatch. Hey, let's make a little parser that does the same thing.
[00:00:18]
parseJSON, I wanna give you the contents, and we'll make a little parseJSON helper, contents and does the exact same thing as that. It just JSON.parse(contents) in the middle. Looks like this is a template for a function I can extract, and just parsing the function but. There we go.
[00:00:43]
What do I do wrong? [LAUGH] So parseJason(contents), did something wrong. Jason.parse(contents). That's right. Because we didn't call this chain. And chain is not a function. I didn't define chain on these. Alright, let's do that. That'd be fun. But, let me just point out that if I didn't fold this, what's happening here is this isn't config.
[00:01:11]
This is another either of config. And so if we just return that right then in there, let's get rid of this. We get a right holding a right. Because what we've done, is right here, that returned us a right of some contents. Then we mapped over that. We took those contents, parsed it.
[00:01:32]
And now you've got to a right, and then parseJSON returned to right. So now we have a Right(Right) of the actual parse contents. That is the, that's like the fundamental thing here. I just want you all to like really understand that, that readFileSync returned us a right of the stuff.
[00:01:52]
Since we mapped, we just pass this in here. Then we parse the JSON, and we ended up with a Right(Right) because we finished the map. So we went back here, and just put this function inside of it. So, by calling chain here, we will fix this problem. So, let's go back to the actual thing there, and define chain.
[00:02:17]
Let's find it for Left and Right. So left chain is awesome. It's just like, thank you for your function, go home. [LAUGH]
>> And then Right is just like box. But this is where fold diverges, because we actually Instead of doing this where we would map and put it back in the box, we expect f to return us a Right.
[00:02:44]
And this is not fold, it is chain. And config is not defined. Where did we lose our variable? I see. Cool. So this basically flattens it as a flat map. Are we okay, cuz that's monads, you're learning monads. [LAUGH] feeling okay about it? map chain, chain flattens apps.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops