Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "Monad Transformers Practices" 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 covers monad transformer practices, and demonstrates how to use the lift method.

Get Unlimited Access Now

Transcript from the "Monad Transformers Practices" Lesson

>> Let's go through these exercises. And we'll talk about free monads, lenses and getting to app building with whatever time we have left for a couple of apps we can build. So I'm just gonna go through this a little quickly but definitely stop me and I'll answer any questions as we go.

[00:00:16] So we use function either ask. Since we're in the function type, we have a ask method we saw earlier to get at this configuration. And then return the port, okay? So I'll say function either.ask, and that is an actual function type. So now I can map over that and get to the config, get to config.port.

[00:00:41] And it works, cool. Not too hard, but I just pull the config out of thin air. It's pretty sweet. Let's take it a little further. Now it says, use example one to get the port and connect to the db. And then we're gonna find whatever this idea is.

[00:01:03] So, just use example one here, call that, it'll give me the port to get the- so this, of course is not the port number. I shouldn't say of course, but this is still inside a function either, right? We just mapped it and changed the value inside to hold the port.

[00:01:22] So we have a function either holding a port. So we call that to map over that to get the port. Neat, and then from there, what do we have to do? We want to connect to the db, so we have connect to db, that takes a port. And then that returns me in either.

[00:01:47] Of the fakeDb or left to failed to connect. I have a function either, and I'm trying to map over that and return an either. So then I'll have a function either either. So we don't wanna do that, we wanna chain and then we wanna lift, This either into the function either, see if that's going all right?

[00:02:16] And the last thing is find the id. We have the id, we have a fakeDb. Now that we have that, we should have a db. And then, we wanna find db.find(id). Crap, it's got another either. [LAUGH] Part of the chain. And we'll have to lift. Let's see if that did it.

[00:02:43] Hey, that passed. Are people seeing why I don't like monad transformers? [LAUGH] But they're everywhere and they're- perhaps they make more sense. I might be making an unfair judgement based on this stuff is fine. Unfair judgement based on factor in Javascript. And we don't have a lot of type help and stuff like that, but they're pretty painful in any language.

[00:03:15] Exercise two. Let's go into ReaderTask, fun. We have a function, holding a task. I just called it reader here because that's usually what you call function. And we're gonna ask again to get the server for the postUrl. And get the server variable from ReaderTask. Boy, what's even happening here?

[00:03:41] [LAUGH] Really sets us up for success y'all. So it looks like server is not defined. We need to get the server. So I call ReaderTask.ask, and we'll chain over that to get the server. And I could have mapped. But I know right off the bat that this thing inside, it's doing a fetch, it's gonna be a task.

[00:04:08] So I'll probably have to just loop through ReaderTask that lift this thing. Does that work? There it is, cool, so, that is a little bit of a- we're talking about getting stuff out of the reader monad, we're trying to put stuff in and we're staying shallow. So it's not as bad here as what we saw over here.

[00:04:31] But I just wanna get a little bit of a feel to use these things. And so we can use this as a tool to build apps when we have very rigid- there's another one, look at that [LAUGH]. Looks like it's already passing. In the interest of time, I don't wanna do this, I just skipped this one.

[00:04:52] I could post the answer later. But what this is trying to explain is what we did together. It's got these either posts, then we're folding. And what we'd rather do is use Api2, which returns us a TaskEither and it would just clean this whole thing up. So it shows where they actually shine.

[00:05:13] Let's do it, why not? [LAUGH] You pulled my leg. So here we just get the post. Wait, does that dot sting make sense [LAUGH] in this context? [LAUGH] Made me pull my arm. And we have Api2 get post. And then we get a post out. Instead of all this, we could just get the post title and be done with it.

[00:05:43] Does that work? It works, cool. So you can see Api2 is much nicer because as I map and chain, I get at the post or whatever it's holding, not the intermediate types in between. And if you do have a type, like a database thing and you make a TaskEither, they do have a place where they're not so bad.

[00:06:04] It's really when you start to have four or five types, you're returning different types and you have to put them in the right slots and so on.