Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Refactoring Using the Either Monad" 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 through a server example how to use the Either monad to refactor code.

Preview
Close

Transcript from the "Refactoring Using the Either Monad" Lesson

[00:00:00]
>> Cool, let's see what happens if I try to require fs ooh, look, it works awesome. Gotta love this app anyway all right [LAUGH] so we got our from nullable. We got our definitions up here, and we're on to this function, we're gonna use either to refactor this function just like we did with box.

[00:00:25]
Here we have a try catch though, seems like a situation where we've this impure, not mathematical function, and what we wanna do is transform it into a mathematical function using either to return us value. And let us deal with it on the calling side, instead of just trying to throw an error when which case like I don't need to call try catch around this.

[00:00:48]
There's nobody telling me I need to do that and like I did it here return to 3000 but if I took this out, it would just blow up. So try catch is pretty, pretty loose so let's go ahead and do this. Let's rename this and keep our pattern what stays on the screen, [SOUND] okay, so the first thing to do here is to say, so, I'm gonna cheat on the purity side to demonstrate this because we don't have any way to capture [LAUGH] this IO yet.

[00:01:25]
But we'll look at that next, so let's put this in an either so we're going to say read the fileFileSync so let's say fromNullable is it they're not? And actually, that could just blow up right? So why don't we do a try catch around that, and try catch is a function that I've defined once, and [SOUND] this is where we gonna bump it down just a little.

[00:01:58]
Save us the trouble all right that's cool is that still readable for y'all? Okay Let's see so what trycatch does, when I call this function, is if this blows up, I'll get a left of the error, and if it doesn't, I get it right so that's pretty neat.

[00:02:19]
Let's go ahead and write, try to catch, let's try catch is a function that takes a function right? And we got to write this once and one time only try we'll call the function, put it in array, if it's happy Return that. Otherwise catch the error and return a left of the error, cool.

[00:02:46]
So that's the one and only time we have to write this and that'd be nice because we don't have to go litter our code with all this syntactical stuff, and deal with like this prickly syntax and the possibility of nobody actually dealing with it. This is what we call the try catch here, then we can map, right, if it works I get file contents, let's see what we did.

[00:03:12]
We parsed down, no, this is gonna blow up possibly so let's first let's just pretend it won't and then we'll deal with that. We'll go ahead and get the config out of that and we'll grab the config.port, terrific. And then once we fold it down where am I dying here?

[00:03:30]
Console log, [SOUND] anybody result is this there is no curly braces, okay, so again we have a rate of 3000, let's just change that, so it's not the same one. [LAUGH] So and then we'll fold it out and if it's a left we'll return, whatever 8080, and if it's a right, we'll just return right?

[00:04:05]
Cool, we get 3000 what if we misspell it? Get 8080, how nice is that? This would have just blown up in our face, but we're told by, right here, and we can actually move this right like we can make read file sync actually let's do that. We'll make a little safer read file, readFileSync, to just be that, right?

[00:04:30]
And it takes a name or a path, and it does that right there Terrific and actually this is wrong, the way this is defined is the path is on the outside. Cuz they don't actually pass the argument to the function, but now we can readFileSync with ['config.json'], there we go.

[00:04:59]
So now just so we're clear, like, I decided to make this because I don't want anyone to ever have the possibility of calling this without try catch. And now they're forced to map over it.

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