Hardcore Functional Programming in JavaScript, v2

Adding Chain for Nested Functors

Brian Lonsdorf

Brian Lonsdorf

Salesforce
Hardcore Functional Programming in JavaScript, v2

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

The "Adding Chain for Nested Functors" 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 write a function that allows for chaining methods and argues that one could use promise.then instead, but in an effort to stay deterministic and mathematical, the use of chaining is better.

Preview
Close

Transcript from the "Adding Chain for Nested Functors" Lesson

[00:00:00]
>> We can we can write a flatten method for box, but it's kind of fold right fold will remove it from the box, effectively kind of flattening it. Maybe instead, we'll be able to write a function. Let's call it chain that allow me to chain boxes. Now chain is also called flat map will allow me to flatten and map in the same step.

[00:00:26]
With JavaScript convention is to call this chain because you're chaining these box operations but you can also see it in flat map is the same idea. So let's go ahead and read chain on box and in that sense, chain is expecting us to return another box. It's gonna expect a box of a box of a value.

[00:00:51]
And so we don't wanna fold it up here, we're just gonna map there. So let's go right chain. And I don't know if you've all, if you're using promise.den, this is very much like promise.den except promise.den will auto-flatten for you whereas chain does not or you have to decide whether you wanna map or chain.

[00:01:15]
With promises, you just called in all the time and if its nested, it flattens it for you, if its not, it doesn't. We like to stay mathematical and deterministic and we don't wanna do different things under the hood. We wanna provide a strictly more powerful API and give you both options.

[00:01:35]
So let's create chain, chain is fun. It's in this case is exactly fold. [LAUGH] The reason for that is, I'm just not gonna put it back in the box. I'm expecting this to return me a box. And I know that fold removes it from a box and chain has a different, it's the same implementation with a different kind of argument contract.

[00:02:02]
We'll see this as we swap out the different instances of functors and monads. That these two start to diverge and become different functions, in this case, it's the same. So that's what we did, if you look at it. We saw a box of four, right? So why don't we just at the end folded out, just take [LAUGH] The value.

[00:02:23]
There we go. So there's all this different ways to manipulate this and write this. I think what we are trying to learn today in the second half of the class is just dealing with working with computations in these containers. There's different API's for them to kind of combine them and pull them out and nest them and, swap their orders and whatnot.

[00:02:50]
And this is what we're gonna be dealing with, a lot of this crap. [LAUGH] But we'll see the benefits here in a minute. So did anybody have any questions over? So we just defined monads right then in there, boom, flatten. So, ask me questions. What are you thinking right now?

[00:03:10]
Are you tripped up on any of this? So.
>> The thing that sold me on our last example was the, you've probably got five eight options for handling price and discount and injecting them into the container, which you'll eventually like build that chain composition. There's a fair number of minimum forms for that.

[00:03:32]
>> Totally right.
>> And that was a sticking point for me as I was trying to think through it.
>> Yeah, which ones would be the best and that's we get back to the beginning like, okay, we have function equivalences. We can turn this into a function that takes a pair and we could process them as a pair.

[00:03:46]
We could do this boxed stuff. All this though is their equivalence and knowing that their equivalent provides you like here's all my options and I know that I can end up at the same spot. So I see that's a tripping point as well a kind of a value add to know the equivalences.

[00:04:07]
But yeah, these functions, running inside the box and then nesting you can see here. We're just using the closure to continue. So if you're thinking your box is saying I'm gonna establish a context and that context has this variable. And then I'm gonna establish a new context and that context has this variable.

[00:04:33]
And now we're in a box and working with both variables. You can kind of, we're not trapping them in those little closures as we go. We're keeping them in a little bit of bigger closure. Incidentally, if you're working with promises, I always reach .then until the situation happens.

[00:04:52]
And then you look for a sync to it, right? Because then you have multiple variables all playing together and you wanna keep in indenting. It turns out that situation doesn't happen all that often though, so.

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