Transcript from the "Avoiding Mutation Solution" Lesson
>> So let's take a look at the solutions to these avoiding mutation exercises. Again, they are in a Fork notebook, you can find by clicking that Fork link at the top of the page of the exercise. So in this solved version, and again, plenty of ways to implement these.
[00:00:18] The main things you wanna make sure that you're doing are not using any of those mutating array methods. You might find that you wanna use some of the non mutating array methods to implement some of these. But you can also get creative and use other things, like perhaps you could use, spread syntax, this dot, dot to implement this first one, the push function.
[00:00:42] So the push function is gonna replace that array.push method, where instead of array.push and then the element being passed in, which is gonna mess with the original array, we're gonna take a push function that's gonna take an element and the array to put it on too. And then we could use the spread syntax to take everything out of the original array into a new array that I'm just creating with this bracket syntax here, and then add on the new element.
[00:01:12] So that's one way we could do this. But again, your mileage may vary, there are plenty of valid solutions to implement this non mutating push. Just make sure you're not adding anything on to the original array in place. And we're checking for that in these little titles. How about the update function?
[00:02:02] And in this case, I'm using some non mutating array methods, slice and concat. Which again, if we really want to force ourselves to write code the way we probably would in a pure functional language, we could create function versions of these, kind of wrap those non mutating methods in a function signature that gives me a little bit more of a functional style.
[00:02:25] But by this point, I think we've gotten a lot of practice on that. So just to make things easier for ourselves, we can use a chain of these methods. So we can first slice off everything before the index that I wanna replace in the array. We can concatenate that with a new little array with the value that I wanna put in at that index, and then concatenate that with the slice of the rest of the array after that index.
[00:04:05] And then the pop function again, which is to replace the mutating myraid pop. We can use the slice function for example, excuse me. We can use the slice method which again does not mutate the original array, and so we can slice off everything up to the next to last element in the array.
[00:04:31] But again, if you can implement a slice function that is going to allow us to get into more of that functional API function calls as the way that we write our code, the style of syntax that we use as opposed to this dot method. That's again, another little challenge that you can try to force yourself to do to complete this in a way that does not involve this little period here.
[00:05:01] And then of course, there are lots of other array methods that we could create similarly, we could create non mutating functions to sort of represent the equivalent operations of. But again, that'll be left as sort of an exercise for you to stretch mind, or if you wanna come back to this in a few days or few weeks and try that that out, perhaps to just keep that muscle memory going.
[00:05:44] The immutable data structures example is here. Again, we'll just let you all kind of digest that on your own time, but I would love to know what differences did people notice? Where there are differences in the two libraries which have a very different way of working with these immutable data structures, right?
[00:06:32] And I'll let you all dig into exactly how it works. But after having just a few minutes to glance at it, would anybody like to share some of their thoughts on differences they noticed or some of their preferences?
[00:06:52] Sort of kinda looks more like using a regular ES6 Map Reduce function. I will just have instead of Array prototype, whenever you now have different options, for us immutable GS can look something like it something entirely new to me.
[00:07:48] So that they could basically work with the features of the language that they're used to. And in fact in other cases, we could actually just kind of destructor this produce function out of the MRI object. I think it's the default value exported by this library is actually the function itself.
[00:08:36] It comes from, I believe the same team that created React at Facebook. So there might be some overlap between the React programming community and the immutable users. So if you're perhaps more comfortable with that world, there might be reasons to use it there. What do we think? Are we interested in maybe trying some of these out, taking a closer look at the docs, trying them out in our own code, seeing if we can really force ourselves to never mutate data?
[00:10:31] You might find that the efficiency improvements afforded by these libraries that use these techniques of persistent data structures and immutable data structures might come in handy, especially when we're working on in situations where performance is more of an issue. Yes, once again, we have not had a ton of time to dive into the world of immutable data structures, but this is an area of functional programming that I think is super fascinating.
[00:10:57] And so I also have a talk linked at the bottom here about how immutable data structures work in detail and why they're more efficient. So if you wanna dive in more and understand a little bit more about how trees and sharing make all of this efficiency possible, you can check that out.
[00:11:14] And there's also going to be lots of resources linked where you can read tons more about how these very fancy, very interesting, very exciting, and really performant data structures work. Are gonna be listed in the resources at the end of the workshop. Did these libraries allow us to work with kind of deeply nested data structures, so that when I create an immutable version of let's say, an array which has other arrays in it, or an object which has other objects as values in it.
[00:11:45] Will it kind of recursively make sure to make everything all the way down immutable? That is a great question. Since I'm not a core maintainer or power user of either of these libraries, I would probably say we should definitely check with their documentation to find out how they handle those nested data structures.
[00:12:11] But my gut understanding is yes, they will help you do that, so that you can, like let's say make an immutable list out of a nested list and still have the deeper nested lists also be locked in place. However, don't quote me on that, go to the source, and check out the docs.
[00:12:59] For example, there's also one for Python, at least one that I know of, and things like that. And always make sure that you dig into those docs to understand what's happening, to understand all the API, and the examples that they'll give you in the docs to see what happens when I'm working with, let's say, a deeply nested data structure.
[00:13:17] That's a great question.