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

The "Refactoring to Dot Chaining" 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 use dot chaining with a functor and refactors the code written in the previous lesson.

Preview
Close

Transcript from the "Refactoring to Dot Chaining" Lesson

[00:00:00]
>> Okay, so again, our job here is to rewrite half the first large number. [LAUGH] These are what the exercise is gonna look like. So kind of get a feel for what we're doing here. It's what you'll be doing. And the idea here is that we're gonna take the x's, we're gonna filter them, I'm gonna call the first divided by two and then we return that.

[00:00:21]
So how do we frame this in terms of box. Let's do the same as we did before. We're gonna rename that. Leave it around for for funsies. I don't know why, it's easier to watch if I do it if I just get rid of it, but please have it up there.

[00:00:38]
So we'll put the x's in the box. And you can skip this, you can go directly to that if you wanted to. But I'm just gonna start there, keep a nice clean workflow. So we're gonna map inside the box. It's kind of weird to have a map and then a filter within.

[00:00:58]
The map because you're probably not used to seeing that but totally works. All right, so now we have found a map again called first and we'll divide that by two. Cool. And then we'll map over this. And return the answer. Look at that answer. So if you haven't noticed we're capturing variable assignment.

[00:01:24]
We have to fold it. Right get it out of the box at the end. So by capturing variable assignment by wrangling these variables and kind of capturing them in these little closures and getting rid of them right afterwards. Got nice control flow, variable syntax, and we're able to create a composition exactly the same as composing.

[00:01:45]
And I could actually, if you want, I could write the tune from, we could actually define compose in terms of box and map, right? So if I said can const compose is equal to, takes our f and our g right. And then we just say box, and we'll return a function so right, it takes, it puts the x in the box and maps f then g.

[00:02:11]
Or it's actually g then f. That's pretty sick, right? We can actually do a compose in terms of folded out I guess, but that'll be composed in terms of box. So, box is strictly more powerful than compositions function, but it captures composition. So you can define it in terms of that.

[00:02:33]
Sweet. Does anybody need a hug?
>> [LAUGH]
>> [LAUGH] All right. This is where the hardcore part comes into play, not the hug the functors.
>> [LAUGH]
>> [LAUGH] Cut myself.

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