Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "The Reader Monad" 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:

Get Unlimited Access Now

[00:00:00]
>> What it's done is unnested it by taking a second argument and passing it in. And if the second argument here is just high again, right, but it's the original high, which is kind of weird, right? We gave it this x, and this one processed it and passed in the transformed x.

[00:00:19] This one's running the original x on the outside. So we get this kind of like x hat or something. It's like the original x. So the exclaim that we would get the lowercase one right? Do that up cuz it's x hat, of course. [LAUGH] let's just go back to x.

[00:00:37] So we get the original x. Let's put this in an array that has the upper one and that one, so we can see both at the same time. Let me get both here. So this behavior doesn't seem very useful upfront, but it will allows us to do is thread kind of invisible environment through our whole program.

[00:00:58] That's why we call this reader, is because x wasn't transformed. x is just kinda carried through and I can just gain access to it whenever I want. So as I start transforming, my input can still get back to this kind of original. Something else that's interesting is we can make a function of, takes x, and we'll just return a function that takes whatever, and just puts the x in there.

[00:01:24] So we can start with a function of Hello. And then we can map to upper on that. Let's do this so we can get some room. What's happening now? This is kind of weird, right? It's going to be hi or hello. We have two arguments now, we got Hello and hi', so what's happening here?

[00:01:47] We're starting with a function with the argument here x as its result, and we're gonna map over that. We can map over it all day long. We can ignore this hi thing, right, this will just be uppercase tillow. But if we want to access the second argument, let's make it a little bit more formal.

[00:02:06] Let's say this is the port 3000, right? If I wanna access my config, all of a sudden I can make a function here inside my function. And that gives me the config. So let's just show, we've got our upper and our config at the same time. Sweet, we're pulling stuff out of thin air, and typically you'll see this kind of framed this way, where we have a function running through.

[00:02:39] And then somewhere in the middle of this this app, I just wanna grab my config and there it comes out of nowhere. I don't know how that works, it's amazing. So let's make one more function to make this a little bit more convenient. And we'll say Fn.ask. And Fn.ask is just already a function that says, okay, I'm going to just give you whatever I'm holding.

[00:03:06] Just like config to config, right? So this is a convenience function that says instead of this bizarre function constructor in the middle of my stuff, I'm able to say, ask, and then I get my config out. And I can return upper in config at the same time. And notice we're doing chain, right, because ask is another function.

[00:03:32] And then we can run it with config and and we blow up, see? Done. [LAUGH] No, what's happening here? ask is not defined, it's Fn.ask. There we go and there we go. This is the reader monad. It's insanely helpful during function architecture. Lately, libraries have started to work way more on this signature of Zio or Rio.

[00:03:56] The idea is that you can do dependency injection in here. I could have not just my configuration but all of my dependencies like my db connection. And whatever my strategy for whatever I wanna use in my local storage or whatever, and then I can just get my environment out.

[00:04:14] And now I have all my my effects. I've just kind of passed in those out of nowhere. So this is a really useful technique that we can use, but that's reader. Super cool. I'm not gonna talk about state, but state is exactly this. It's like the same exact thing, except we're able to thread a state through and modify it.

[00:04:36] And this one doesn't let you modify it. I just don't find it as useful maybe in JavaScript as others do. But we can talk about that later.