Functional-Light JavaScript, v3

Impurity Exercise: Wrappers & Adapters

Kyle Simpson

Kyle Simpson

You Don't Know JS
Functional-Light JavaScript, v3

Check out a free preview of the full Functional-Light JavaScript, v3 course

The "Impurity Exercise: Wrappers & Adapters" Lesson is part of the full, Functional-Light JavaScript, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle instructs students to refactor functions in order to contain the existing impurity using wrapper functions and adapter functions.

Preview
Close

Transcript from the "Impurity Exercise: Wrappers & Adapters" Lesson

[00:00:00]
>> Kyle Simpson: Well, let's practice that topic of containing function impurity. We talked about two different techniques, so we have a two part exercise on impurity. First part of this exercise, it's laid out here in the read me, and I encourage you definitely do read this, but essentially you're gonna be taking some functions and moving them around, or refactoring them, so that they.

[00:00:23]
The first technique that you're gonna use is that technique we talked about, of wrapping a function around, wrapping a pure function around an impure function. So if you notice in our excercise, EXJS, you'll notice we have these functions like sortStudentsByName and sortStudentsByID. And you'll notice that these functions modify a variable outside of themselves.

[00:00:47]
So there is our side effect, the fact that students.sort is being called, that modifies the array in place, and that's the side effect that we want to avoid. So our first task, part one, is to take this function and to re-factor the code around this function, so that it is behaving in a pure way from the outside.

[00:01:10]
The way you're gonna do that actually is to have, or to define a function called getStudentsByName. So this is the function that you're gonna modify, and in this case most likely move, to wrap around the sortStudentsByName. And you'll notice, in the read me it'll explain, it needs to receive input and it's going to return the modified output, but it will contain that impurity to itself, so it will not actually end up changing the results of these particular students.

[00:01:40]
You'll notice at the bottom of your exercise file, there's a variety of console statements, and these console statements have comparisons in them. When your tests pass, essentially, each of these sections of console log statements will print true. So you want to see the getStudentsByName, when you're passing students it should produce a return result that prints true for all of these.

[00:02:08]
That's part one. Also, make sure that you don't break the current vales that happen, or these should all print true, as well. So studentTest1 and studentTest3 should all print trues. These won't print true until part two, okay? So that is part one, make sure you check the read me for some hints, some specific things that you wanna do, you want to avoid actually changing these functions, so don't change sortStudentsByName, simply wrap something around it, that's how you want to avoid the side effects.

[00:02:42]
For part two of the exercise, we wanna do something similar, we wanna contain the impurity inside of the sortStudentsByID, we wanna contain that impurity. But when we practice the second technique we talked about, which wasn't wrapping a function around it, but rather creating an adapter function alongside it.

[00:03:01]
So remember, we have this getStudentsByID, that's gonna be our adapter function, we wanna have it sitting alongside the sortStudentsByID. And remember, it's in the slides, and you can check that, remember the six steps that you basically need to accomplish. You need to save the current state, set up the appropriate initial state, call the side effect, capture the new state, restore the old state and then return the captured state.

[00:03:28]
So make sure you handle all of those steps in your adapter function. And at this point, once you've solved that, all 15 of these tests should print true.

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