Welcome in his lecture, I'm going to talk about how to shrink a slice using slice expressions.
Let's talk about how they work.
Before talking about the expressions, let me tell you first what a sliceable value is.
A sliceable value is any value that can be sliced using a slice expression, a slice value on an array
value.
Both are sliceable values because you can slice them.
A shrink value is also a sliceable value, by the way.
It is because a street value is a bitts lies behind the scenes.
All right.
Now let's take a look at what a slice expression looks like.
First, I need to type which value?
I want a slice.
Then I need to type square brackets with a column.
Then I need to give it a starting position, and lastly, I need to give it a stopping position, this
expression Salinas's the sliceable from the starting position until to the top position.
The starting position should be the index of an element inside the slice.
So it will start at zero because indexes start from zero.
On the other hand, the stop position should describe the position of an element, not its index.
So it should start from one instead of zero.
Actually, this is an index too, but it is easier to think of it as the element position instead.
OK, so in the end these expressions will return a new value that describes the slightest segment of
the original slice.
All right, let's take a look at a few examples.
He was a less.
This creates a slice that looks like this.
Are you ready to slice it?
OK, let's do it.
This last expression starts the slicing from index zero to the first element, in the end, it returns
a new slice value that looks like this.
These are different slices, sourceless expression returns.
You are newsless value using the original slice.
OK.
Now let's list the original slice up to its second ealand, these returns, a new slice value with the
first two elements of the original slice.
As you can see, the start position describes an index inside the slice.
However, the opposition describes the elements position, so it slices the slice, starting from its
first index up to its second element.
Let's slice it up to its third element.
Now.
This returns a new value with the first three elements of the original slice.
Now let's slice it up to its fourth element like this.
This returns a new slice with the first four elements of the original slice.
Now, let's slice it up to its last element like this.
This creates a similar slice, by the way, round slicing from the first element.
You can throw it away like this.
And when slicing up to the last element, you can also throw it away like this, so this soulless expression
is equal to this.
So whenever you see one of the positions are missing now, you know what they are equal to.
Let's slice it beyond its length.
Can I do that?
Let's see.
It doesn't work.
There is a runtime error slice bounce out of range, so you cannot slice a slice beyond its length.
OK, by the way, you don't have to slice a slice beginning from its first element.
Of course you can slice it however you like.
Let's slice it by admitting the first and the last elements like this.
This will create a new value like this one.
Or you can slice it up to the last element like so as I said, I don't need to type the stop position
because it is equal to the last element by default.
So this sless expansion is equal to this sless expression on the right.
Here's the question.
What would happen if I opened to a sliced slice value?
First, I'm going to slice it up, these fourth elements like this, so it creates a new slice with
four elements.
Now I'm going to appoint an exclamation mark to the new slice, by the way, these exclamation mark
is not important here.
It could have been any other character as well.
Now let's see what happens.
It returns a new slice by adding the new element.
It does so because the new slices lacked for so it pants the new elements right after the length of
the slice as the fifth element.
But this has a side effect.
So let's check out the last element of the original slice.
Now it will disappear.
And instead, it gets replaced with the new element, so the apparent function here or writes the original
slice, too, if Einstein has seen this, he will call it the spooky action at a distance, I call it
it's expected is because the new slice and the original slice are connected behind the scenes.
I'll talk about this in detail in the next lecture series so there is more to the slices than meets
the eye.
All right.
Let's take a look at our coding example.
Let's say you have fetched search results from a database or from a public API, and you'll receive
the slice such as this one.
All right, let me print it using my predecessor library.
By the way, don't forget importing the package here.
All right.
These are the elements inside the items list.
Last item for the top three elements.
First, I'm going to declare a newsless variable.
Then I'm going to slice the items beginning from the first index to the third element like this.
Remember, slicing returns, a new slice value.
So I can say it in this variable.
By the way, as I said, they're slicing from the first index.
You can drop it like this.
They also print it.
Here are the top three items from the original slice.
Now, let's slice it for the last four items.
To do that, I need to slice it from the 9th index to the last item like this.
Remember, the items slice has 13 elements, so its last element is at the 13th position.
I can also drop the last element position like this.
Let me also print it.
All right, limerence.
Cool, it has sliced the last four items, by the way, round slicing, I can also use a variable first.
I'm going to say the length of the items sliced to the variable.
Then I'm going to get the last four items by replacing the nine here with the L minus four.
And minus four is equal to nine, so this expression is equal to the previous one.
OK.
As you can see, the result is the same.
Let's take a look at what happens if I slice the last four slice again, for example, let me get its
elements at the middle, starting from the first index to the third item like this.
OK.
Print it.
As you can see, you can slice a slice indefinitely, slicing your slice is called re slicing.
OK, here, the last four slices indexes look interesting.
For example, the defender element is at the zero index in the last slice, but it is at the ninth index
in the item slice.
Let me print the ninth index of the items slice and the first index of the last four slice.
As you can see, both indexes point to the same element, but their indexes are different.
You can also see this by looking at the indexes here.
So in summary, the same elements come in different indexes in different slices, depending on their
position in those slices.
So indexes are relative to a slice like.
Now I'm going to show you the differences between slicing and indexing, let me print the type and the
values of the items.
OK, let's slice it for the Tetris element.
Tetris is that the second index, but in the third position.
OK, now I'm going to get the Tetris element directly by using an index expression.
As you can see, Selous expression returns a value, even it has a single element in it.
However, the index expression returns the element value directly.
So in summary, then use a slide expression even for a single item, the returned value will be a slice
value, not an element value, but values an index expression.
You will get an element value, not a slice value.
OK, let me tell you one last thing.
Men use a class expression.
It returns a slice value with the same type of the original value, for example, here.
That is why the items slice and the type of the returned values from this slice.
Expressions are the same.
They all are string slices.
OK, all right.
That's all for now.
In the next lecture, I'm going to show you a pagination example using sliced expressions.
So you there are.