Functional-Light JavaScript, v3

Impurity Solution: 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 Solution: 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 live codes the solution to the adapters section of the exercise.


Transcript from the "Impurity Solution: Adapters" Lesson

>> Kyle Simpson: Okay, let's talk about the solution for part two of the impurity exercise. What we said was we wanna take this getStudentsByID and make it as an adapter function to the sortStudentsByID so that it will contain, if you will, it will prevent from leaking out any of the side effects that the sortStudentsByID would have.

In fact, the side effect being that it would modify that global students array. So because we're not containing the lexical scope here, what we're gonna have to do is that brute-force technique, that six steps, where we're gonna have to take the current snapshot of everything, and then restore it afterwards.

That's our basic approach. So getStudentsByID is gonna receive and existing list of students, we'll call that curStudents, and that's gonna be the list we set as our initial value. But before we set an initial value, we need to capture the existing state of the global students. I'm going to call that origStudents, and that'll be students.slice.

That's a copy, actually, I need to do a slice because it's gonna actually modify it and we need to keep a copy of the array. If we only had a reference to the array, then we would have a reference to the modified array, which isn't what we want.

So we're making a backup copy, if you will, of students before we modify it, and then we're gonna set that as step one. Step two is to set up the initial state for the students. So we wanna say students = curStudent, we could, for good measure, make a copy of curStudents.

So let's go ahead and do that. It doesn't really matter in this particular case, but let's go ahead and, just for good measure, we'll make a copy of curStudents. So now we have the original state set up, we have the original state saved, initial state set up and we wanna call them sortStudentsByID.

And sortStudentsByID automatically returns to us a value so we can do all in one step basically steps four, or step three and step four, which is to call it, make the modification and capture the new value, we wanna call that new students.
>> Kyle Simpson: That'll be the newly modified, newly sorted order students.

Step five, we need to restore the previous state. So we need to say, students = origStudents, and here, we don't need to make another copy since we're just restoring. And step six is to return that new students state that we capture.
>> Kyle Simpson: Let's take a look and seeing how this code behaves and we should see all true is on our test cases.

What mistake did I make, sortStudentsByID.
>> Speaker 2: The ID is capital D 2
>> Kyle Simpson: There we go. Thank you very much. Let's try it again. There we go, all truth, okay. [LAUGH]

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