Transcript from the "Immutability Exercise" Lesson

[00:00:00]

>> 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.

[00:00:23] 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.

[00:00:41] 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.

[00:01:02] 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.

[00:01:20] 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.

[00:01:38] 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.

[00:01:56] 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.