WEBVTT 00:01.730 --> 00:02.260 Welcome. 00:03.080 --> 00:07.190 Until now, you have created slicers only by using literals. 00:07.550 --> 00:10.160 Actually, there is another function called the make. 00:10.310 --> 00:15.580 It allows you to allocate the back injury or fossilise with the given length and capacity. 00:15.830 --> 00:21.860 So why there is a function called the make is because a slice doesn't have a defined length up front. 00:21.860 --> 00:22.240 Right. 00:22.730 --> 00:26.410 So the make function allows you to create a slice with a defined length for you. 00:26.900 --> 00:29.920 But don't be mistaken that its length becomes fixed. 00:29.930 --> 00:31.280 You can still open to it. 00:31.670 --> 00:32.420 Let's take it out. 00:32.840 --> 00:37.270 The main function is just another building function so you can call it from anywhere. 00:37.490 --> 00:41.900 You just need to type make and then tell it which type of a slice you want to make. 00:42.470 --> 00:46.100 Then you need to tell it the length of the slice that you want to create. 00:46.370 --> 00:50.080 This second argument also describes the capacity of the back injury. 00:50.450 --> 00:54.070 So the return slice, this length and capacity become equal. 00:54.440 --> 00:55.010 That's it. 00:55.190 --> 00:59.840 Then the make function will do the rest and return you a slice like this one here. 00:59.840 --> 01:03.680 It creates an insulin's with three empty elements and returns it back. 01:04.100 --> 01:07.540 As you can see, it sets the elements to their zero values. 01:08.240 --> 01:11.210 You might be asking why should I use the make function? 01:11.210 --> 01:14.400 What I can create slices using a less literal already? 01:14.990 --> 01:21.230 Well, as you know, when a slice needs to grow and if there isn't enough capacity, the append function 01:21.230 --> 01:22.970 allocates a new back injury. 01:23.450 --> 01:26.140 And as you know, that's an expensive operation. 01:26.690 --> 01:32.140 So you can prevent it by allocating a large enough back injury, by using the make function. 01:32.600 --> 01:37.420 You can also use the make function to create a slice with a different length and capacity. 01:37.940 --> 01:41.180 This is needed and you want to use on this one part of a slice. 01:41.780 --> 01:47.840 So when you grow it, it won't allocate a new way if you define its capacity wisely, because here it 01:47.840 --> 01:49.820 creates a slice with the length of three. 01:49.910 --> 01:55.040 Right now I'm going to add another argument to set the returning slices capacity laakso. 01:57.510 --> 02:03.900 When I do saw the make function creates a new slice like this, the return slice is length and covers, 02:03.900 --> 02:04.880 they are not equal. 02:05.250 --> 02:09.050 As you can see, it's like this tree, but its capacity is five. 02:09.300 --> 02:13.220 So this slice can be extended up to five elements afterward. 02:13.740 --> 02:19.620 The one common pattern of using the make function is passing the zero lengths like this so that you 02:19.620 --> 02:21.930 can easily append new elements to the slice. 02:21.930 --> 02:25.440 From its beginning here, I saved this new slice to the variable. 02:25.920 --> 02:28.150 Now I'm going to expand the new element to the slice. 02:28.590 --> 02:29.930 Let's see what happens next. 02:30.870 --> 02:36.630 As you can see, the apparent function adds the new element as the first element, remember the apan 02:36.630 --> 02:39.660 function at Pince after the length of the given slice. 02:40.050 --> 02:42.580 So here the slices length was zero. 02:42.930 --> 02:46.470 So it has a pattern that the new value to the beginning of the slice. 02:47.090 --> 02:51.210 OK, in theory, let's take a look at an example in the ed now. 02:54.300 --> 02:58.350 First of all, let me tell you that you don't need to use the make function. 02:58.560 --> 02:59.580 Yeah, that's right. 02:59.880 --> 03:04.170 We gofers use the make function when we want to optimize our software. 03:05.570 --> 03:11.900 All right, let's say I have received a string slice such as this one, imagine that our database has 03:11.900 --> 03:15.440 only three tasks for now, but it can grow in time. 03:15.950 --> 03:20.850 So this slice has only three elements, but it could have thousands of elements as well. 03:21.140 --> 03:25.100 Anyway, let's say I want to change all the tasks to uppercase. 03:25.310 --> 03:27.110 I want to do it in a new slice. 03:27.350 --> 03:31.100 It is because I don't want to override the original tasks here. 03:31.490 --> 03:37.640 Then I'm going to arrange over the original tasks like so here I'm going to add part to the up tasks. 03:40.620 --> 03:46.620 And now I'm going to use the strings to upper function to make the next task uppercase like this. 03:46.840 --> 03:48.660 OK, let me also bring the up task. 03:49.470 --> 03:50.820 Let's first printed here. 03:53.780 --> 03:56.490 And one more inside the loop here, OK? 03:56.660 --> 03:57.340 That's right. 03:58.000 --> 03:59.050 Well, it works. 03:59.480 --> 04:01.640 However, this court is inefficient. 04:01.820 --> 04:06.760 It allocates three different back injuries each time I packed up tasks less. 04:07.580 --> 04:08.320 Do you know why? 04:08.990 --> 04:11.180 Please post the video and try to guess. 04:16.410 --> 04:22.560 It does so because when there isn't enough capacity in the back, correct, the apparent function creates 04:22.560 --> 04:23.600 a new back injury. 04:24.580 --> 04:27.370 So how can I make this court more efficient? 04:27.550 --> 04:31.060 I mean, how can I prevent array allocations each time? 04:31.060 --> 04:31.720 I can't? 04:32.150 --> 04:35.730 Well, that is where the main function comes into the scene. 04:36.190 --> 04:41.860 You know, using the make function, you can create a slice with a pre allocated Badgingarra with the 04:41.860 --> 04:42.610 given size. 04:42.850 --> 04:46.210 So now I'm going to change the literal here like this. 04:46.540 --> 04:52.720 Here I'm creating a string sliced and I'm going to type land tasks here because I want to create a slice 04:52.720 --> 04:55.170 that is equal to the length of the test. 04:55.870 --> 04:58.180 OK, you'll see why in a second. 04:58.360 --> 05:01.010 Now let's see what it looks like this time. 05:01.030 --> 05:03.870 At first it creates a slice with three elements. 05:03.910 --> 05:07.630 So its length is three, its capacity is also three. 05:07.870 --> 05:12.790 It is because this argument sets both the length and the capacity of the new slice. 05:13.240 --> 05:15.840 Also, pay attention to the content of the elements. 05:16.300 --> 05:17.260 All are zero. 05:17.440 --> 05:21.400 That is because the make function sets the elements to their zero values. 05:21.610 --> 05:22.840 All are ready to use. 05:23.020 --> 05:25.360 However, an interesting thing happens here. 05:25.840 --> 05:31.480 Instead of adding the new elements to the beginning of the slice, the append function a stamp to the 05:31.480 --> 05:32.560 back of the slice. 05:33.100 --> 05:35.140 Do you know why it is possible? 05:35.350 --> 05:36.250 And try to guess. 05:41.210 --> 05:47.210 It does so because, as you know, the aband function upon the elements after the length of a slice 05:47.550 --> 05:50.610 at first here, the slices length was three. 05:50.990 --> 05:57.200 So the next opponent has expanded the new elements as the fourth element, and there wasn't enough space 05:57.200 --> 05:57.640 in the back. 05:57.740 --> 05:58.140 Correct. 05:58.190 --> 06:01.270 So the append function has created a new back injury. 06:01.700 --> 06:04.250 But this is the problem that we want to avoid. 06:04.400 --> 06:05.720 So how can we fix it? 06:06.230 --> 06:10.940 Well, I can simply set the elements by using their index positions like this. 06:12.740 --> 06:15.530 I also need to enable the index variable here. 06:17.400 --> 06:23.770 Cool, now it works, the pointers are the same, of course, if I pants one more element like this, 06:23.790 --> 06:25.580 it will create a new back injury. 06:26.720 --> 06:27.340 IMNSHO. 06:28.400 --> 06:32.840 Now, the pointer has changed, so the opponent has allocated a new era. 06:33.560 --> 06:34.970 OK, let me remove this. 06:35.360 --> 06:38.120 By the way, this court is kind of cumbersome. 06:38.550 --> 06:42.160 It's because I need to keep track of the index of the elements here. 06:42.440 --> 06:44.860 So how can I use the append function instead? 06:45.230 --> 06:48.880 Remember the aband function a pince at the back of a slice. 06:49.370 --> 06:54.800 So if I set the length of the slice to zero, I can use the apan function to do that. 06:54.810 --> 06:56.180 I'm going to type zero here. 06:56.450 --> 06:59.260 Here zero means the length of the slice. 07:00.200 --> 07:03.730 But this third argument means the capacity of the slice. 07:03.920 --> 07:07.870 So this make function will create a slice with zero length. 07:07.880 --> 07:12.620 But with that capacity of three is because the length of the task slice is three. 07:14.480 --> 07:18.500 While there was an error, why this past video and try to guess? 07:23.570 --> 07:30.050 OK, let's take a look at the length of the slice, it is zero, and as I've defined it here, its capacity 07:30.050 --> 07:30.610 is three. 07:30.890 --> 07:35.570 So even though it has three elements in its back injury, it cannot use them yet. 07:35.780 --> 07:42.200 So if I extend this slice like so it can use its full capacity here, I'm using the cap function. 07:42.540 --> 07:45.860 Remember, the cap function returns the capacity of a slice. 07:45.950 --> 07:53.450 So this expression basically equal to the Selous expression without the first index, I'm just slicing 07:53.450 --> 07:56.100 it up to its last element in its back injury. 07:57.090 --> 07:58.730 Anyway, let's try this one. 08:00.120 --> 08:07.320 Cool, now it works, however, this isn't the best cold yet I still cannot use the apan function here. 08:07.590 --> 08:10.470 This card is equal to the previous version of the code. 08:10.620 --> 08:11.960 Let's fix the problems now. 08:12.390 --> 08:14.700 First, I'm going to remove these regionalizing. 08:15.610 --> 08:21.430 Then I'm going to get rid of this index variable and lastly, instead of using the index variable, 08:21.550 --> 08:24.040 I'm going to use the append function like so. 08:25.090 --> 08:26.730 OK, let's try this one. 08:27.700 --> 08:29.320 Now, this court is awesome. 08:29.650 --> 08:35.230 It doesn't allocate a new worry and I don't need to introduce and use the index variable, I'm just 08:35.230 --> 08:37.570 using our old friend aband function. 08:37.870 --> 08:39.260 So how does it work? 08:39.280 --> 08:43.790 Remember, again, the apan function at pant's after the length of a slice. 08:44.140 --> 08:47.390 So at first the length of this slice was zero. 08:47.590 --> 08:51.750 So that pan function has expanded the new element as the first element. 08:52.210 --> 08:55.450 There was enough capacity, so it has used the same array. 08:55.600 --> 08:57.400 Then the length became one. 08:57.850 --> 09:03.640 So that Panth function has expanded the new element as the second element, right after the first element 09:03.910 --> 09:04.590 and so on. 09:05.200 --> 09:10.080 As you can see, you can use the make function, you the length of the new slice. 09:10.600 --> 09:16.480 You can use it to allocate a new baking korei with any length and capacity that you need. 09:17.450 --> 09:18.810 All right, that's all for now. 09:19.070 --> 09:19.910 Thank you for watching. 09:20.210 --> 09:21.300 See you in the next lecture.