Functional-Light JavaScript, v3

Immutability Exercise

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 "Immutability Exercise" 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 define pure functions dealing with data structures that are not meant to be changed when passed into a function.


Transcript from the "Immutability Exercise" Lesson

>> Kyle Simpson: Let's try an exercise on this topic of immutability. Get some practice with both assignment immutability as well as value immutability and of a couple of different kinds. We'll look first at read only data structures and then we'll also look at not changing a data structure that's been passed into the function.

This exercise won't be covering actual immutable data structures like immutable js. But hopefully, these concepts will transfer into working with something like immutable chance. In this exercise, you're gonna be generating a set of lottery numbers. And because we're doing generation of lottery numbers, of course, it's random generation.

The goal here is to make everything else pure except for that call that is doing a random number generation. We wanna leave that everything else using all of the principles that we've learned so far in the course about function purity. So if I switch over to the exercise file, you'll notice that we have a function that does the generation of a random lottery number.

That's okay that that one is impure, but we want to leave everything else according to our principles. So we're going to fill out a definition for a pick number function. And you'll notice that I have a wild true loop there that supposed to pick a number and stick it in in some way into the lucky lottery numbers array.

Now, that is pretty easy, we could just stick the numbers in. But the Read Me details a set of constraints. First of all, we want no duplicate lottery numbers. Now, that's not all that likely that you would randomly generate the same lottery number. We wanna ensure that they are no duplicates, so we wanna have unique lottery numbers.

Also additionally, we want our pick number to make sure that lucky lottery numbers always stays sorted in ascending order. So essentially, what you're gonna want to do is have pick number generate a number and stick it into the lucky lottery numbers list. If it's not already there and then make sure the list is sorted.

Now, keep in mind, that if pick number calls lottery known directly, that makes pick number not a pure function. So, remember what we talked about, about extracting impurity, we don't want to embed the impurity if there's someway to extract it. So, keep that in mind also with the exercise.

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