Welcome in his lecture, I'm going to talk about the internals of slices, starting with the backing
of a slice, let's declare a slice with three elements like this.
Do you think that the stores, these elements, the answer is a big, big no.
It's because a slice doesn't track the store.
Any elements?
Let me show you.
Here's a small part of the computer memory behind the scenes as the last literal creates an era and
returns a slice value that refers to that era.
It is called the baking array of a slice, so the bakery stores, the elements, not the slice.
By the way, the baking array is stored separately from a slice so it can be shared among multiple slices.
Let me show you an example to make you understand what I mean.
By the way, from now on, whenever I say array, please note that I'm talking about the baking.
Here, the blue slice can see the entire elements of its array, right, that slice the ages slice like
this.
Now the red slice is a window only to the first element of the array.
Let's slice the edges, slice again, so the green slice is a window or the last two elements of the
as you can see, a slice expression doesn't create a new baking or a beginners usually tend to get a
slice.
Expression creates a new era, but it doesn't do so.
It just creates a new slice value that is referring only some part of the same baking core values,
a slice expression.
The back of the original slice will be shared.
For example, here I created the red and green slices through the blue slice.
So that is why all the slices here share the same array.
OK, here is a question.
What would happen if I change an element in one of those slices?
Let's change the first element of the age.
A slice.
So the last assignment changes the beginning, the first element, so all the slices also change because
they all are looking at the same array, but the green slice doesn't change because it's only knows
about the last two elements of their.
OK, there's also change the second element of the blue slice like this.
This time last assignment changes only the blue and the green slicers.
Let's change the last element of the blue slice.
Here, the last assignment only changes the blue and the green slices.
By the way, slicing creates a new slice value, but it doesn't copy the original slices data into the
return and slice it because it doesn't contain any elements on its own.
It just refers to its begging erase data so there is nothing to be copied.
That is why slicing is a very fast and cheap operation.
Remember, areas are contiguous values in memory, so they allow fast access to their elements as Celeste
uses an array under the hood, so indexing a slice is also very cheap.
OK, let's get back to the first example and let's talk about separate back injuries.
Let me create a new slice.
So it creates a new era.
So the blue slice uses the purple colored area, but the green slice uses the teal colored area, if
you change the blue slice, the green slice won't see it because there are separate.
This means that a slice, literal, at least with one element, always creates a new baking array.
As an example, let me create an IRA.
Let me convert it to a slice beginning from its first element to its last element, doing so creates
a new value.
Let me create two more slices using the edge of slice value, slice an array.
It becomes the beginning of the return slice to show you that, let me change some of the elements of
the age of slices.
The last assignments also change the edges array because it is the beginning of the age slice.
The last assignments also affect all the other slices here, because they are begging Gary is the same.
It is the Asira I created the ages slice by slicing the edges so the eight slices vegging becomes the
edges.
And in turn I created the red and the green slices by slicing the edges slice.
So they are begging or also becomes the edges.
All right, let's take a look at an example in the calling, Ed.
Let me first import my pretty slice package.
OK, let's say I received the grades of a students as an array from a database like this.
Let me print it by slicing it.
So this Comverse, the area to a slice on the fly.
Let me also set two options that are special to my package.
This print backing option brings the back injuries of the slices.
And the Mux Pearline.
It brings seven elements from the slices in a single line, as you can see, the printed slice refers
to the great story behind the scenes.
However, there was a problem in the transmission.
I was expecting a source of data, but I received the first three elements in the wrong order, but
the rest is fine.
The solve that I want to sort only the first three elements to do that.
I'm going to slice it like so.
Let me also show you the Francis.
Those grey boxes are the rest of the elements inside the front, slicers backing away.
Remember, the front glass is Pechanga is the great Serrie.
So those grey boxes are stored by the greats.
There are three more elements in the back injury, but the Fraunces only sees the first three elements.
It is because I created the front slice by slicing the first three elements of the great array on OK.
Also notice that the grates and the front slices are looking at the same baking array.
The great sorry, you can see that by looking at their pizza or in other words, their pointer feels
here.
I'll talk about the pointer field in detail in the next lecture's hours.
So behind the scenes, the front variable contains a value that refers to the great array to prove this
to you.
Now I'm going to sort it using the sort packages floor six to force function like this.
Let me show you its documentation.
As you can see, it takes the floor 64 slice and it doesn't return anything.
Soon you'll understand how a function can change a slice even though it doesn't return that slice.
All right.
The first parts of the grades are right and the front change, that is because I use a class expression.
The return slices array will be the same with the sliced value.
So here the sliced value is the grade three and the return slice is the front slice.
So they have the same making.
OK.
By the way, the same thing is also true for the slices, of course.
Let me duplicate and comment out this array first.
Now I'm going to change it to a slice type by removing its length from its type like this.
So now the grade is not an array.
It's a slice variable.
As you can see, there are no errors because the slices and array is almost act like the same literalness.
The output is the same.
This proves that it doesn't matter whether you slice an array or a slice.
In the end, a slice expression always creates a slice with the same baking array of the sliced value.
OK, here is a question.
Can I break this connection between the grades and the front slices?
I mean, can I prevent them using the same making sure they show you to do that?
I'm going to create a new Nhill slice like this.
Then I'm going to appoint all the elements of the great slice to this new great slice like this lame
also changed this expression to use the new great slice instead.
OK, let me run it.
As you can see, only the front changes, but the great slice stays the same, it is because now the
front and the great slices are not connected.
So they don't share the same back in Korea anymore.
Now, the front Selassie's era is the same with the new great slice.
It is because I created the front slice from the new great slice.
But the great slices begging is a heathen error created by its slice little.
Let also print the new great slice.
As you can see, great slice is the same, but the new grates and the front slices are not, they have
changed.
It is because they have the same backing behind the scenes.
By the way, I can directly use a needle slice here, let me show you let me comment about the new great
slice first.
Now I'm going to remove the new great slice from this app, and instead I'm going to convert a new value
to our floor 64 slice value like this Engo.
New value is a timeless value, so I cannot pass it to the append function directly because the apan
function expects a slice.
That is what I need to convert the new value to a slice type first.
This is just a for 64 slice on the fly.
OK, limerence.
As you can see, the output is the same.
All right, as the last example, let me slice the front glass like this, I'm also going to create
one more slice value by assigning it to the front three slice like.
So let me also print the front to.
Front three.
OK, let's take a look here, the front front two, front three, and the new great slices are connected.
That is why sorting differences affects all of them.
This is because they are all looking at the same back injury or death.
So for now, to understand this lecture better, you need the soul, the exercises, of course.
Thank you for watching.
See in the next lecture by.