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

The "Interpreters" 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 adds interpreters to the different free monads. Interpreters allow developers to log information about how the Free monad runs.


Transcript from the "Interpreters" Lesson

>> I will spend one more second before I shift this thing and move on to the next one. Unless you have any wishes here before we move on. But what else we can do here we can actually step this out, right? So we can interpreter test. Could do interpretTest here, and instead of dbToTask what about dbToId, right?

Instead of consoleToTask, what about consoleToId? And we could write a little couple functions here. So we have consoleToTask, where Instead of actually params ID, instead of actually reading the question getting the input, we can say Id of, it's going to be a function here of whatever the question was q say answer to q.

All right, get this out of my way. It's gotta be horribly frustrating for people online. Sorry about these pop-ups everywhere cool. So there is question. Let me actually break this out in other word [LAUGH] rating some actual stuff. And what's gonna happen here for the output, this well, I've got some, I've got a string and I can do Id of writing the string as.

All right keep doing that, remember I'm gonna be interpolating so you could see what's going on. Cool, so what we're doing is, returning an ID that kind of logs what's happening. And it's not actually writing to the screen. We're getting data back. If we do DB to Id, we can do the same thing on save and an all oops.

And that takes our table both times. Tip on a record query. And again, saving our table. And here we'll finding all in the table with a query. Great, see what happens here with our test interpreter. Just blows up. [LAUGH] I'm still forking it. [LAUGH] So Id and we're just going to extract.

And then from there, we're going to run our recursively on that and see what happens. Eddie's not fine, I gotta bring it in. Boo Boo. This is about the time of the day where this is just gonna be air after after air. F is not a function and interpretTest, just don't even know where f is.

So we're gonna run that. Are we back to,
>> Ruby start and data function.
>> So we're extracting whatever is in the Id. And remember before we made this promise that these things were going to be returning like, well, I think they're returning tasks of the continuation. And as we run these things and chain these things, we're actually running and chaining the free mon ad, which is being interpreted into a different kind of result.

So that should still hold like dbSave and all that is just turning them into Ids now. I think I'm just, I'm confused what f is right now. This will be the last little thing. Just look at this console.logf. Once again, back to I should just left this in the first place, right?

[SOUND] Okay, what is f? F is undefined router route. There we go. See what happened. I didn't interpret correctly on my question is the answer to and it's supposed to be a route. Just for fun let's just do right let's see what happens. [SOUND] [LAUGH] Blew the stack.

[LAUGH] But you can see, it is indeed interpreting it. We just have to do a little bit more work arounds. Okay, now that we're not going to task and doing this question answer interface, how do I mock out. I think what I would do next before we shut this is to say well, I have a answer to every question, kinda pre canned in my test framework, and I'll just look up my answers table to the cube.

Then what's happening is we're actually writing the valid route back when I asked the right questions. Printing should work and then it's interesting, something we may not have noticed is the fixed data point is a recursive data structure as we see it just blew this deck. We just call this again and again and again right?

And the way to stop a free mon ad, you have a few constructors in here lift f puts you in a free, but you also have pure. And pure is a way to make your constructor that's the end and it will just stop. So we could do that, but we wont, you know, it's there.

And this is a cool way to do it. We've written an app and we've written it in terms of free monads. And we've made multiple interpreters. So all as well. Okay, do we have any questions on this?
>> Can you scroll back up to the top? I'm sure I just, it was triggered by the one of the last things that we added.

We had to pull in what was the Id monad. Yes, the fixed points type. Essentially, we have to have some sort of exit condition.
>> Yes.
>> The loops. So whether that's a type or it's a logical construction or something, we need to have some way of cueing the recursion to.

>> Exactly, and actually that's exactly. So you start with fix. And we have these functors have either have a fixed point functor or you don't. A list is a fixed point functor, cuz it will have a list inside a list inside a list until you hit null, right?

And null is the fixed point. The way fix works is it injects itself in between each of these things. And free does the exact same thing just provides you with pure to stop. So you have your functor and then you go to free you're going to functor and get free and then you finally hit your pure and you stop.

So it gives you it's just fixed with an endpoint to 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