WEBVTT 00:01.960 --> 00:07.680 Welcome in his lecture, I'm going to talk about the internals of slices, starting with the backing 00:07.990 --> 00:12.490 of a slice, let's declare a slice with three elements like this. 00:13.200 --> 00:19.450 Do you think that the stores, these elements, the answer is a big, big no. 00:20.020 --> 00:22.710 It's because a slice doesn't track the store. 00:22.720 --> 00:23.650 Any elements? 00:24.490 --> 00:25.150 Let me show you. 00:26.700 --> 00:33.900 Here's a small part of the computer memory behind the scenes as the last literal creates an era and 00:33.900 --> 00:37.340 returns a slice value that refers to that era. 00:38.220 --> 00:45.240 It is called the baking array of a slice, so the bakery stores, the elements, not the slice. 00:46.020 --> 00:54.330 By the way, the baking array is stored separately from a slice so it can be shared among multiple slices. 00:55.020 --> 00:58.200 Let me show you an example to make you understand what I mean. 00:58.890 --> 01:04.620 By the way, from now on, whenever I say array, please note that I'm talking about the baking. 01:06.370 --> 01:13.960 Here, the blue slice can see the entire elements of its array, right, that slice the ages slice like 01:13.960 --> 01:14.310 this. 01:15.130 --> 01:19.210 Now the red slice is a window only to the first element of the array. 01:20.270 --> 01:26.940 Let's slice the edges, slice again, so the green slice is a window or the last two elements of the 01:27.980 --> 01:35.240 as you can see, a slice expression doesn't create a new baking or a beginners usually tend to get a 01:35.240 --> 01:35.620 slice. 01:35.650 --> 01:39.680 Expression creates a new era, but it doesn't do so. 01:40.220 --> 01:47.450 It just creates a new slice value that is referring only some part of the same baking core values, 01:47.450 --> 01:48.530 a slice expression. 01:48.710 --> 01:51.600 The back of the original slice will be shared. 01:52.190 --> 01:56.890 For example, here I created the red and green slices through the blue slice. 01:56.900 --> 02:00.380 So that is why all the slices here share the same array. 02:01.280 --> 02:02.570 OK, here is a question. 02:03.140 --> 02:07.460 What would happen if I change an element in one of those slices? 02:08.210 --> 02:10.460 Let's change the first element of the age. 02:10.460 --> 02:11.060 A slice. 02:13.230 --> 02:20.640 So the last assignment changes the beginning, the first element, so all the slices also change because 02:20.640 --> 02:26.520 they all are looking at the same array, but the green slice doesn't change because it's only knows 02:26.520 --> 02:28.670 about the last two elements of their. 02:29.820 --> 02:33.900 OK, there's also change the second element of the blue slice like this. 02:34.850 --> 02:39.500 This time last assignment changes only the blue and the green slicers. 02:40.550 --> 02:43.100 Let's change the last element of the blue slice. 02:44.330 --> 02:48.740 Here, the last assignment only changes the blue and the green slices. 02:49.310 --> 02:56.690 By the way, slicing creates a new slice value, but it doesn't copy the original slices data into the 02:56.690 --> 03:01.380 return and slice it because it doesn't contain any elements on its own. 03:01.760 --> 03:06.210 It just refers to its begging erase data so there is nothing to be copied. 03:06.500 --> 03:09.790 That is why slicing is a very fast and cheap operation. 03:11.670 --> 03:19.500 Remember, areas are contiguous values in memory, so they allow fast access to their elements as Celeste 03:19.500 --> 03:24.510 uses an array under the hood, so indexing a slice is also very cheap. 03:25.580 --> 03:30.350 OK, let's get back to the first example and let's talk about separate back injuries. 03:32.120 --> 03:33.670 Let me create a new slice. 03:34.910 --> 03:36.350 So it creates a new era. 03:38.370 --> 03:46.260 So the blue slice uses the purple colored area, but the green slice uses the teal colored area, if 03:46.260 --> 03:52.150 you change the blue slice, the green slice won't see it because there are separate. 03:52.320 --> 03:58.100 This means that a slice, literal, at least with one element, always creates a new baking array. 03:59.620 --> 04:01.750 As an example, let me create an IRA. 04:03.710 --> 04:10.280 Let me convert it to a slice beginning from its first element to its last element, doing so creates 04:10.280 --> 04:11.220 a new value. 04:11.990 --> 04:17.480 Let me create two more slices using the edge of slice value, slice an array. 04:17.600 --> 04:24.410 It becomes the beginning of the return slice to show you that, let me change some of the elements of 04:24.410 --> 04:25.420 the age of slices. 04:26.090 --> 04:32.700 The last assignments also change the edges array because it is the beginning of the age slice. 04:33.840 --> 04:40.140 The last assignments also affect all the other slices here, because they are begging Gary is the same. 04:40.530 --> 04:49.170 It is the Asira I created the ages slice by slicing the edges so the eight slices vegging becomes the 04:49.170 --> 04:49.620 edges. 04:50.580 --> 04:55.620 And in turn I created the red and the green slices by slicing the edges slice. 04:56.040 --> 04:58.980 So they are begging or also becomes the edges. 05:00.620 --> 05:03.620 All right, let's take a look at an example in the calling, Ed. 05:08.250 --> 05:10.730 Let me first import my pretty slice package. 05:11.050 --> 05:17.130 OK, let's say I received the grades of a students as an array from a database like this. 05:18.250 --> 05:20.020 Let me print it by slicing it. 05:21.200 --> 05:24.150 So this Comverse, the area to a slice on the fly. 05:24.740 --> 05:28.570 Let me also set two options that are special to my package. 05:29.270 --> 05:32.810 This print backing option brings the back injuries of the slices. 05:33.980 --> 05:35.270 And the Mux Pearline. 05:36.770 --> 05:43.430 It brings seven elements from the slices in a single line, as you can see, the printed slice refers 05:43.430 --> 05:45.530 to the great story behind the scenes. 05:46.380 --> 05:48.670 However, there was a problem in the transmission. 05:49.140 --> 05:54.870 I was expecting a source of data, but I received the first three elements in the wrong order, but 05:54.870 --> 05:55.910 the rest is fine. 05:56.550 --> 06:01.200 The solve that I want to sort only the first three elements to do that. 06:01.350 --> 06:02.790 I'm going to slice it like so. 06:04.150 --> 06:06.270 Let me also show you the Francis. 06:07.890 --> 06:12.740 Those grey boxes are the rest of the elements inside the front, slicers backing away. 06:13.380 --> 06:16.920 Remember, the front glass is Pechanga is the great Serrie. 06:17.310 --> 06:20.190 So those grey boxes are stored by the greats. 06:21.420 --> 06:26.640 There are three more elements in the back injury, but the Fraunces only sees the first three elements. 06:27.160 --> 06:33.450 It is because I created the front slice by slicing the first three elements of the great array on OK. 06:34.170 --> 06:39.180 Also notice that the grates and the front slices are looking at the same baking array. 06:39.330 --> 06:45.260 The great sorry, you can see that by looking at their pizza or in other words, their pointer feels 06:45.270 --> 06:45.530 here. 06:45.990 --> 06:50.100 I'll talk about the pointer field in detail in the next lecture's hours. 06:51.020 --> 06:57.800 So behind the scenes, the front variable contains a value that refers to the great array to prove this 06:57.800 --> 06:58.220 to you. 06:58.370 --> 07:02.690 Now I'm going to sort it using the sort packages floor six to force function like this. 07:03.080 --> 07:04.670 Let me show you its documentation. 07:05.060 --> 07:08.940 As you can see, it takes the floor 64 slice and it doesn't return anything. 07:09.650 --> 07:14.870 Soon you'll understand how a function can change a slice even though it doesn't return that slice. 07:15.530 --> 07:15.890 All right. 07:16.370 --> 07:22.160 The first parts of the grades are right and the front change, that is because I use a class expression. 07:22.160 --> 07:26.040 The return slices array will be the same with the sliced value. 07:26.660 --> 07:32.530 So here the sliced value is the grade three and the return slice is the front slice. 07:33.080 --> 07:34.970 So they have the same making. 07:35.480 --> 07:35.750 OK. 07:36.650 --> 07:40.320 By the way, the same thing is also true for the slices, of course. 07:40.790 --> 07:43.460 Let me duplicate and comment out this array first. 07:43.940 --> 07:48.640 Now I'm going to change it to a slice type by removing its length from its type like this. 07:49.070 --> 07:51.050 So now the grade is not an array. 07:51.080 --> 07:52.210 It's a slice variable. 07:52.670 --> 07:58.850 As you can see, there are no errors because the slices and array is almost act like the same literalness. 07:59.420 --> 08:00.680 The output is the same. 08:01.190 --> 08:05.170 This proves that it doesn't matter whether you slice an array or a slice. 08:05.450 --> 08:11.750 In the end, a slice expression always creates a slice with the same baking array of the sliced value. 08:11.990 --> 08:13.810 OK, here is a question. 08:14.330 --> 08:17.970 Can I break this connection between the grades and the front slices? 08:18.530 --> 08:24.720 I mean, can I prevent them using the same making sure they show you to do that? 08:24.740 --> 08:27.410 I'm going to create a new Nhill slice like this. 08:28.500 --> 08:34.700 Then I'm going to appoint all the elements of the great slice to this new great slice like this lame 08:34.710 --> 08:39.030 also changed this expression to use the new great slice instead. 08:39.780 --> 08:40.920 OK, let me run it. 08:42.520 --> 08:49.360 As you can see, only the front changes, but the great slice stays the same, it is because now the 08:49.360 --> 08:52.000 front and the great slices are not connected. 08:52.420 --> 08:55.030 So they don't share the same back in Korea anymore. 08:55.510 --> 08:59.170 Now, the front Selassie's era is the same with the new great slice. 08:59.680 --> 09:03.880 It is because I created the front slice from the new great slice. 09:04.240 --> 09:09.460 But the great slices begging is a heathen error created by its slice little. 09:10.430 --> 09:12.320 Let also print the new great slice. 09:15.860 --> 09:21.830 As you can see, great slice is the same, but the new grates and the front slices are not, they have 09:21.830 --> 09:22.420 changed. 09:22.880 --> 09:26.060 It is because they have the same backing behind the scenes. 09:26.910 --> 09:32.370 By the way, I can directly use a needle slice here, let me show you let me comment about the new great 09:32.370 --> 09:33.220 slice first. 09:33.660 --> 09:38.790 Now I'm going to remove the new great slice from this app, and instead I'm going to convert a new value 09:38.790 --> 09:42.540 to our floor 64 slice value like this Engo. 09:42.780 --> 09:49.020 New value is a timeless value, so I cannot pass it to the append function directly because the apan 09:49.020 --> 09:50.570 function expects a slice. 09:50.910 --> 09:54.600 That is what I need to convert the new value to a slice type first. 09:54.960 --> 09:57.960 This is just a for 64 slice on the fly. 09:58.230 --> 09:59.910 OK, limerence. 10:00.810 --> 10:02.790 As you can see, the output is the same. 10:03.780 --> 10:10.500 All right, as the last example, let me slice the front glass like this, I'm also going to create 10:10.500 --> 10:14.250 one more slice value by assigning it to the front three slice like. 10:14.260 --> 10:17.730 So let me also print the front to. 10:19.420 --> 10:20.020 Front three. 10:20.380 --> 10:28.050 OK, let's take a look here, the front front two, front three, and the new great slices are connected. 10:28.540 --> 10:31.720 That is why sorting differences affects all of them. 10:32.110 --> 10:36.790 This is because they are all looking at the same back injury or death. 10:36.800 --> 10:41.760 So for now, to understand this lecture better, you need the soul, the exercises, of course. 10:42.490 --> 10:43.480 Thank you for watching. 10:43.510 --> 10:44.830 See in the next lecture by.