WEBVTT 00:01.930 --> 00:03.260 Welcome back. 00:03.310 --> 00:09.070 Working with slices, you don't have to copy the elements using a loop for that, there is a built in 00:09.070 --> 00:10.450 function called the copy. 00:10.960 --> 00:13.870 It copies the elements of a slice to another slice. 00:14.140 --> 00:15.400 Let's see how it works. 00:15.820 --> 00:18.280 First, you need to type copy that. 00:18.310 --> 00:19.180 You need to pass it. 00:19.180 --> 00:23.260 A destination slice which will receive the elements from the source slice. 00:23.950 --> 00:27.070 So you also need to pass it a source slice like this. 00:27.560 --> 00:32.880 So the copy function will copy the source slices elements to the destination slice. 00:33.460 --> 00:39.460 By the way, the element types of both of the slices should be identical or the copy function won't 00:39.460 --> 00:39.800 work. 00:40.570 --> 00:43.900 All right, let me show you how the copy function works in detail. 00:45.140 --> 00:46.250 Here is a bite size. 00:48.440 --> 00:49.340 Here is another one. 00:52.100 --> 00:55.520 As you can see, the element type of these slices, is it? 00:55.730 --> 01:02.150 OK, let me pass these slices to the copy function so the green slice will be copied to the blue slice 01:02.150 --> 01:02.690 like this. 01:04.800 --> 01:11.550 You might be asking why the copy's only two elements instead of three is because the copy function only 01:11.550 --> 01:15.570 copies the elements based on the length of the smallest slice. 01:15.990 --> 01:20.630 The blue slice has two elements, but the green slice has three elements. 01:21.090 --> 01:24.000 So the slice at the left is the smallest slice. 01:24.600 --> 01:28.290 That is why the copy function copies on two elements here. 01:28.710 --> 01:30.630 The copy function also returns. 01:30.630 --> 01:35.760 How many elements it copies like this so you can know how many elements are copied. 01:36.120 --> 01:38.400 Let's take a look at an example in decoding Ed. 01:40.620 --> 01:46.380 OK, let's say I'm creating a program that prince training probabilities for the next day's, let's 01:46.380 --> 01:50.310 say I have received these training probabilities from a remote API. 01:50.880 --> 01:52.590 Now I'm going to print the data slice. 01:56.020 --> 01:57.370 The data looks good. 01:57.620 --> 02:00.070 OK, now let's calculate the remaining probability. 02:05.100 --> 02:07.750 Here, I'm going to use the person sign twice. 02:08.160 --> 02:14.610 That's because I want to print just a single person sign here so the printer will not interpret it as 02:14.610 --> 02:15.060 a verb. 02:17.010 --> 02:20.340 Now I'm going to calculate the reigning average manually like this. 02:24.230 --> 02:28.580 And lastly, I'm going to divide the total by the length of the slice like this. 02:29.300 --> 02:35.840 Remember, the land function returns and int so I need to convert it to offload six to four like this. 02:38.080 --> 02:45.220 OK, it says that there is a 29 percent chance of raining so we can go out and play the weather is beautiful, 02:45.520 --> 02:52.930 awesome, but not so fast because I received new information, I learned that a hacker has corrupted 02:52.930 --> 02:55.640 the first two elements while I'm receiving the data. 02:55.810 --> 02:59.530 Fortunately, I have received new data for the first two elements. 03:01.990 --> 03:09.050 OK, now I need to copy to the original data, I can do so by manually overriding the data like this. 03:09.350 --> 03:11.440 However, this is cumbersome, right? 03:11.590 --> 03:14.460 And in real life, the data could have been longer. 03:14.500 --> 03:16.270 So let's use a loop instead. 03:16.270 --> 03:18.880 I'm going to loop over the new data slice like this. 03:19.790 --> 03:25.450 Now I'm going to copy the new data from the new data slice to the corrupted data in the data slice like 03:25.450 --> 03:25.850 this. 03:25.900 --> 03:26.440 All right. 03:26.470 --> 03:27.070 Limerence. 03:28.090 --> 03:32.410 It works, but there is nothing magical here, right, instead of a loop. 03:32.440 --> 03:33.970 Let's use the copy function. 03:34.150 --> 03:36.360 Let me remove the loop first here. 03:36.370 --> 03:40.600 I'm going to copy to the data slice from this new slice like this. 03:40.970 --> 03:43.090 Let me also remove this declaration. 03:43.790 --> 03:50.290 Remember, a composite literal, like a slice, literal here creates a new value so I can use it directly 03:50.290 --> 03:51.860 without assigning to a variable. 03:52.000 --> 03:52.860 Let me run it again. 03:54.020 --> 03:57.130 It works cool, the weather will probably be rainy. 03:57.300 --> 04:02.590 All those nasty hackers, anyway, as you can see, the last two elements are the same. 04:02.840 --> 04:03.440 But why? 04:04.100 --> 04:10.220 Well, it's because the cuppy function finds the smallest slice and it copies only that many elements. 04:10.790 --> 04:15.200 So here is Newsless is the smallest one because it has only two elements. 04:15.410 --> 04:17.840 But the data slice here has four elements. 04:18.060 --> 04:22.700 So the cubie function only copies two elements from the new slice through data slice. 04:23.060 --> 04:27.740 Let's see what happens if the original slice is greater than the destination slice. 04:27.920 --> 04:33.200 Now I received five more data points for the new reigning probabilities like these ones. 04:33.890 --> 04:35.060 OK, let's try it. 04:35.930 --> 04:40.300 As you can see, this copying operation completely overwrites the data slice. 04:40.850 --> 04:46.100 That's because the new slice has five elements, but the data slice has four elements. 04:46.350 --> 04:51.380 So the copy function copy is the four elements from the new slice to the data slice. 04:52.190 --> 04:56.660 It also returns how many elements it copies limn, also printed. 05:00.740 --> 05:06.220 As you can see, it prints for it's because the copy function has copied four elements. 05:06.740 --> 05:09.110 So the last data point here is lost. 05:09.500 --> 05:13.750 This means that you cannot create new data elements using the copy function. 05:14.120 --> 05:17.600 It just copies the values from a slice to another one. 05:18.150 --> 05:22.430 You want to grow the slice, you need to use the apan function like this. 05:24.360 --> 05:31.510 Remember, this creates a new value with zero length so I can expand to the beginning of the data. 05:32.070 --> 05:34.500 Now I'm going to pass the new data to here. 05:36.380 --> 05:41.600 As you can see now, the data slice is bigger than before, so it has a new back injury. 05:42.080 --> 05:44.840 Anyway, our focus is the cuppy function here. 05:45.200 --> 05:47.930 So I'm going to commence out the last bands like this. 05:48.600 --> 05:51.500 OK, let's say I want to say the original data. 05:51.530 --> 05:56.510 To do that, I can create a new slice using a make function like this here. 05:56.540 --> 06:01.940 I'm going to pass the length of the data slice to create a new slice with the same length or the original 06:01.940 --> 06:02.560 data slice. 06:02.930 --> 06:08.630 Now I can use the copy function to copy all the elements from the data slice to the new slice like this. 06:08.870 --> 06:13.220 It will copy all the elements because these slices have the same length. 06:13.370 --> 06:16.670 OK, let me also print the saved slice like this. 06:20.030 --> 06:20.330 Cool. 06:20.590 --> 06:22.480 I saved the original data. 06:22.670 --> 06:28.520 Now I can work on the data slice without affecting the safe slice, it is because they have different 06:28.520 --> 06:28.900 backing. 06:29.540 --> 06:33.410 For example, I can change the data slices first element like this. 06:35.300 --> 06:41.510 As you can see, only the data slice sees the change, not the saved slice, by the way, most of the 06:41.510 --> 06:46.700 time I don't use the copy function for cloning a slice because it is cumbersome to write. 06:46.910 --> 06:51.720 First, you need to call the make function that you need to call the copy function instead. 06:51.740 --> 06:58.790 I can clone a slice using the apan function like this here, spanning all the elements of the data slice 06:58.790 --> 06:59.720 to a new slice. 06:59.850 --> 07:03.190 Remember, the append function can only work with slices. 07:03.500 --> 07:08.090 So that is why I'm converting the normal slice to our floor 64 slice here. 07:08.300 --> 07:14.780 Also remember, a new slice doesn't have a back injury, so the apan function creates a new Babchenko 07:14.780 --> 07:15.320 right here. 07:15.350 --> 07:18.860 So the saved slice here, references to that new array. 07:19.160 --> 07:25.610 As you can see, it works as before the safe slice and the data sless have different back injuries. 07:25.670 --> 07:29.050 That's why changing one of them doesn't affect the other one. 07:29.300 --> 07:35.000 These two ways are almost equal, but I prefer the second way because it is more concise. 07:35.670 --> 07:36.040 Cool. 07:36.260 --> 07:37.060 That's all for now. 07:37.430 --> 07:38.260 Thank you for watching. 07:38.540 --> 07:39.460 Seeing the next picture.