WEBVTT 00:06.370 --> 00:12.870 In this video you're going to learn some very important new concepts in iOS 6 which include iterate 00:12.890 --> 00:17.830 as with the next method generators or generator functions. 00:18.310 --> 00:21.530 So to start let's create an array. 00:21.820 --> 00:23.650 So I'm gonna call it baskets. 00:25.830 --> 00:29.410 And let's add some items to our baskets. 00:29.580 --> 00:31.200 So I'm gonna have strawberry 00:34.180 --> 00:49.240 apple you'll and read so you already know how to loop over the array and perform all sorts of things. 00:49.880 --> 00:55.070 So when you loop over the array This is a form of iteration. 00:55.070 --> 00:55.850 So for example 01:01.280 --> 01:07.040 you can do things like logging the elements of the array All right. 01:08.270 --> 01:13.910 So we are looping over the array and we are performing an inspiration. 01:13.910 --> 01:16.490 So this is what we call iteration. 01:16.610 --> 01:24.620 Now the array is called and it's arable because it defines its iteration behavior. 01:24.830 --> 01:28.480 When using a loop like this one. 01:28.500 --> 01:29.180 Right. 01:29.240 --> 01:30.200 So our array 01:35.180 --> 01:39.930 basket is a eatable All right. 01:39.940 --> 01:45.740 And here we are performing a loophole of iteration. 01:46.000 --> 01:48.600 So it's very important to understand these concepts. 01:48.840 --> 01:49.050 Okay. 01:49.090 --> 01:50.920 So an array is in trouble. 01:52.300 --> 02:03.940 So this is a good example of a javascript built in type which has its own default iteration behavior. 02:03.940 --> 02:04.370 All right. 02:05.020 --> 02:08.800 So other objects don't have that. 02:08.800 --> 02:14.500 We'll see later some data structures like mob sets and so on which do have this feature. 02:14.740 --> 02:22.190 And so they are in trouble just like our array right. 02:22.220 --> 02:26.510 So in E6 there is a new concept called an iterator 02:30.590 --> 02:37.930 so an iterator can help you loop through an area will object like an array. 02:37.970 --> 02:46.800 So here we've got our array the iterator is going to be a separate objects which will help you loop 02:47.610 --> 02:50.040 over the array. 02:50.130 --> 02:57.990 Now the iterator can access items of your array or your collection. 02:57.990 --> 03:05.870 It can also keep track of its current position within the array. 03:05.910 --> 03:13.220 It also has got a method called Next this method will return. 03:13.240 --> 03:17.550 The next item in the sequence starting from the first one. 03:17.590 --> 03:22.390 So when you call next for the first time it's going to return the first item and then equally again 03:22.420 --> 03:23.020 is going to return. 03:23.020 --> 03:28.820 The second item and so on until you reach the end. 03:28.870 --> 03:36.610 So next is gonna return something as far as you've got items still left in the iterator. 03:36.880 --> 03:44.440 So every time you call it is going to return an object with a couple of properties value the value of 03:44.440 --> 03:47.450 the item and done. 03:47.710 --> 03:50.780 Which is gonna be either false or true. 03:50.830 --> 03:59.320 So if items are still available and you return an item then done is going to be false if there are new 03:59.380 --> 04:03.490 items left in the iterator then done is going to be false. 04:04.300 --> 04:12.660 Now an easy way to understand this is by looking at a built in iterator. 04:12.660 --> 04:13.170 All right. 04:13.180 --> 04:19.910 So there is a method which creates an iterator out of there right in Essex. 04:20.170 --> 04:30.970 So that is going to be a built in method creating an iterator out of an array so 04:33.910 --> 04:41.980 let's call it s array iterator like this and the method is entries. 04:42.250 --> 04:50.020 So the entries method returns a new array iterator object that contains the key value pairs for 04:52.960 --> 04:56.020 each index in the array. 04:56.030 --> 04:56.250 All right. 04:56.260 --> 05:07.240 So let's try it and see what we get. 05:07.760 --> 05:11.060 So we can see that you've got an array iterator. 05:11.060 --> 05:11.590 All right. 05:11.630 --> 05:19.670 So now what we need to do we need to start calling the method next of the iterator just to understand 05:20.270 --> 05:21.890 what's going on. 05:21.890 --> 05:23.870 So I'm gonna log 05:27.090 --> 05:30.270 the outputs of the next method like this. 05:30.840 --> 05:37.260 So you can see now that we are having an object with a couple of properties value and done. 05:37.440 --> 05:43.380 So this is the first item the value is an array. 05:43.530 --> 05:47.410 It's got a couple of elements zero and strawberry. 05:47.880 --> 05:56.830 So this is referring to the index and the value of the first item now to present things clearly you 05:56.830 --> 05:59.260 can use a jace on string 06:02.710 --> 06:03.650 like this. 06:05.000 --> 06:08.940 Now you can see the value of this property. 06:08.950 --> 06:13.390 It s an S S an array and done is false. 06:13.390 --> 06:16.360 Now if you call next again you get the next item. 06:17.780 --> 06:18.050 Again 06:21.830 --> 06:22.540 and again 06:29.360 --> 06:39.230 and if you call it now you get only done true because there are new items left in the iterator. 06:40.320 --> 06:40.840 All right. 06:40.960 --> 06:47.910 So this represents how we defined an iterator of the beginning and how we define the next method. 06:47.950 --> 06:53.120 This is a really good example to understand the concept of an iterator. 06:53.230 --> 06:59.070 So again when we called next for the first time it returned the first item. 06:59.110 --> 07:00.150 Then we called it again. 07:00.160 --> 07:05.500 It was certainly second item and we can carried on until the end where there are there are new items 07:05.500 --> 07:05.960 left. 07:06.100 --> 07:11.260 And if we send done said with a value true. 07:11.700 --> 07:11.960 All right. 07:11.960 --> 07:17.930 So now here is a quick challenge for you. 07:17.930 --> 07:21.730 So you would need to build a function 07:26.230 --> 07:28.830 that makes an iterator also with every 07:39.000 --> 07:45.420 so you can call this function something like create iterator. 07:45.870 --> 07:53.280 And it takes an array and is going to return an iterator objects. 07:53.410 --> 07:57.790 And remember the iterator objects has got to have a method next. 07:57.790 --> 08:05.810 So when you return the objects it's going to be like something like this with a next method inside. 08:06.380 --> 08:15.790 So now the values that you return when calling the next method they don't have to be like this. 08:15.910 --> 08:25.210 So in our case we call the method entries and the values are arrays showing the index and the value. 08:25.210 --> 08:32.680 Now if you try instead of entries you tried the values 08:35.740 --> 08:43.240 then it s going to return and iterator and instead of the index on the value it gets only the value 08:49.740 --> 08:50.110 okay. 08:50.170 --> 08:50.590 So 08:54.780 --> 08:56.180 it's coming this way now. 09:01.070 --> 09:05.060 So now we can see only the values. 09:05.060 --> 09:15.840 Now there is another option you can return the indexes using the keys method all right. 09:15.920 --> 09:20.730 So instead of values we are getting the keys set for now. 09:20.770 --> 09:23.410 Just keep its values. 09:23.470 --> 09:29.140 Let's say that you want to create a function that returns something like this 09:34.860 --> 09:38.810 so I suggest you pause the video here. 09:38.940 --> 09:43.810 Create the function and come back once you're done all right. 09:43.820 --> 09:46.160 So hopefully you manage to find the solution. 09:46.310 --> 09:51.350 So let's use the E6 syntax instead of this one 10:04.220 --> 10:12.090 so as you can guess we're going to return a closure here because we need to keep track of the index. 10:12.410 --> 10:19.370 So we're gonna have a variable index defined just before the return statements 10:22.820 --> 10:29.150 so the next function is going to be a closure which has close access to its outer environment where 10:29.150 --> 10:31.800 we storing the index variable. 10:32.700 --> 10:36.770 So let's define next. 10:36.770 --> 10:39.570 So let's use the iOS 6 same sex again 10:43.120 --> 10:55.240 so first we're going to check if the index is still within the arrays length so if the index is less 10:55.330 --> 11:00.730 than the length of the array then we're going to return something like this. 11:00.730 --> 11:03.340 If the index becomes too big. 11:03.340 --> 11:09.970 This means that we reached the end of the array and that means that we gonna be returning done said 11:10.210 --> 11:10.810 then. 11:11.080 --> 11:12.310 True. 11:12.430 --> 11:29.710 So if the index is less than the array or the arrays length we're going to return the object value and 11:30.040 --> 11:31.560 done like this. 11:31.810 --> 11:32.410 Otherwise 11:35.570 --> 11:39.420 we are going to return done 11:54.690 --> 12:01.450 so the value here is going to be the element of index index. 12:01.460 --> 12:01.750 All right 12:04.750 --> 12:13.370 which is array and then index and then done set to false 12:20.920 --> 12:30.070 now every time we return something we're gonna need to increment the index by 1. 12:30.780 --> 12:44.200 Now the problem is we've got a written statement here so you can either define a new variable and call 12:44.200 --> 12:56.230 it something where you store the index then use temp right there and increment index like this otherwise 12:56.860 --> 13:00.340 you can use the same code we used before. 13:01.030 --> 13:09.320 And once you use index discard a plus plus there and this will increment index. 13:09.490 --> 13:17.880 All right so let's try and allocate I'm going to define a new iterator. 13:17.890 --> 13:20.860 Call it array iterator to 13:24.000 --> 13:26.990 only gonna use 0 0 function creates iterator 13:29.550 --> 13:37.130 and our arguments this time is going to be a simple array like that's so I'm using costs here we can 13:37.130 --> 13:40.860 use concepts there as well because the iterator doesn't change actually. 13:41.480 --> 13:45.800 So now if you log 13:49.660 --> 13:54.870 the outputs of the next method this is what you get. 13:54.970 --> 13:55.170 Right. 13:55.180 --> 14:01.720 So while you 1 and False then again maybe you 2 and again 14:04.560 --> 14:05.510 value 3. 14:05.760 --> 14:12.370 And finally you get done to perfect 14:15.420 --> 14:22.880 now E6 comes with some special functions which define iterative algorithms. 14:23.040 --> 14:31.140 So this will spare you the time of creating functions like create iterator so the special functions 14:31.200 --> 14:36.000 are called Generator functions or generates is 14:41.770 --> 14:44.320 so these generator functions. 14:44.320 --> 14:50.520 They are actually a factory of iterate as they create iterations basically. 14:50.580 --> 14:58.150 So this creates a generates a function that returns it raises so you will need to use the keyword function 14:59.820 --> 15:01.920 start like this. 15:01.920 --> 15:08.230 So whenever you see function star then you would need to know that it is actually creating iterator. 15:08.490 --> 15:13.520 So let's call this function generates iterator. 15:13.680 --> 15:15.070 All right. 15:15.670 --> 15:27.080 So what's gonna happen now is that we're going to have a few lines of code and we're going to ask the 15:27.140 --> 15:33.340 function to return a or yield a certain value and ports. 15:33.740 --> 15:42.720 And this will imitates or this will create actually the behavior of the iterator. 15:43.630 --> 15:49.750 So what's gonna happen is that we're going to have one line of code and then we're going to yield something 15:50.800 --> 15:53.990 and you understand what yielding means exactly. 15:54.440 --> 16:03.310 So for example let's just log something like running and then yield one. 16:03.310 --> 16:13.430 So here we are actually pausing the generator and we do that using the keyword yield. 16:13.510 --> 16:19.510 This is the value that you will get when you use the next method. 16:19.510 --> 16:30.220 And then if you carry on and you log running again and then yield another value 16:34.120 --> 16:37.540 that value is going to be thrown. 16:37.660 --> 16:39.760 If you call the next method again. 16:39.760 --> 16:50.900 So let's try this so let's create an iterator call it s it s and use the generator function to create 16:50.990 --> 16:52.560 that iterator. 16:52.700 --> 16:53.840 So this call next. 16:53.840 --> 16:59.080 First let's see what we get. 16:59.370 --> 16:59.910 Here we go. 16:59.910 --> 17:08.250 So the face value is one and done is false so we are posing right now. 17:08.700 --> 17:11.430 Let's call it again. 17:11.630 --> 17:17.370 Now the value is 2 which is that's 1 and done is false. 17:17.390 --> 17:26.380 Now if you call us again we don't have any other yield statements and this time the value is undefined 17:26.910 --> 17:34.060 and done is true meaning that we came to the end of our iterator. 17:34.690 --> 17:44.940 So again this is a simple function creating iterator is so the variable its stores and iterator which 17:45.000 --> 17:47.130 we created using that function. 17:47.430 --> 17:53.580 And you can see that when you call the method next you see the same behavior that you would get if you 17:53.580 --> 18:00.810 call or you create an iterator out of an array using one of the methods does recovered earlier. 18:02.070 --> 18:07.350 So this was a quick introduction to generators in E6. 18:07.350 --> 18:18.160 Now in coming videos you will see some points in advance section where we cover generators in even more 18:18.160 --> 18:20.110 detail. 18:20.110 --> 18:28.170 But now you can just remember that when you create a generator function like this you can actually run 18:29.010 --> 18:33.750 a few lines of code then pause at some points. 18:33.750 --> 18:34.470 All right. 18:34.470 --> 18:42.470 So here we run the first line and we paused and we returned this value and done objects. 18:42.570 --> 18:49.880 And then when we called Next again we run that line of code and then we returned to and false. 18:49.950 --> 18:53.960 And at the end we just returned and defined and true.