WEBVTT 00:01.100 --> 00:05.930 Hi in this episode we will learn what IQ is and how to implement it. 00:06.020 --> 00:10.530 Also after our implementation is done we will reduce it using channels. 00:10.550 --> 00:14.620 A very powerful idea that will help us build concurrent applications. 00:15.110 --> 00:21.460 So IQ is very similar to this type of language learned that on the previous episode it's basically our 00:21.470 --> 00:30.260 data abstractions which allow us to process data so the general idea of a queue is that I in queue items 00:30.320 --> 00:34.710 into the queue and I did queue items from the front of the queue. 00:34.790 --> 00:45.120 So it is our queue will in queue which is analogous to push from the start that would learn on the previous 00:45.120 --> 00:45.570 episode. 00:45.660 --> 00:47.820 So in this case we have a number of one in the queue. 00:47.820 --> 00:52.360 We push them to put number three four five six. 00:52.670 --> 01:00.930 But rather than popping items which will be six five four three and two we did queue them which is taking 01:00.990 --> 01:02.550 the oldest item first. 01:02.550 --> 01:06.330 In this case two three four five and six. 01:06.920 --> 01:07.570 OK. 01:08.010 --> 01:11.850 So let's review the previous stack which is like this. 01:11.860 --> 01:18.180 You remember you push items which will be analogous to in queue rather than shopping which is taking 01:18.180 --> 01:21.990 the newest with the queue then just taking the orders. 01:21.990 --> 01:22.470 OK. 01:22.620 --> 01:25.370 So let's apply our implementation of this. 01:25.380 --> 01:27.840 So very pretty similar. 01:27.840 --> 01:33.830 So I wanted to first is to define our queue structure which will have again items and items is going 01:33.840 --> 01:36.320 to be a slice of integers. 01:36.330 --> 01:42.440 Remember you can choose any time you want and we know that we need to do. 01:42.530 --> 01:43.470 Thank you. 01:43.470 --> 01:49.280 These will receive an I and II type and I'm going to return anything. 01:49.440 --> 01:54.740 And we also need the queue method. 01:54.740 --> 01:55.310 Sorry. 01:55.310 --> 02:01.450 The queue here which is not receiving anything and is returning on into it right. 02:01.490 --> 02:05.850 Let me think those errors this queue is capital queue. 02:06.470 --> 02:06.770 OK. 02:07.100 --> 02:16.120 So what we know is that we will be a new queue here and we can call in queue 1 in Q2. 02:16.250 --> 02:18.030 And let's say 3. 02:18.500 --> 02:27.740 And then when we do queue them we will need to get the elements in the same order that we queue them 02:28.010 --> 02:28.400 right. 02:28.880 --> 02:35.560 So these like a queue of a bank the first that comes to the queue is the first to get serviced. 02:36.890 --> 02:42.280 Let me just run this to make sure nothing is broken yet and it's fine. 02:42.320 --> 02:48.830 We're just returning 0 because our implementation sense here that the queue will just reach and zero 02:48.920 --> 02:49.820 and a queue does. 02:49.820 --> 02:53.590 Basically I'm not just an empty implementation. 02:53.690 --> 03:00.080 So let's start by implementing and queuing and this is really easy it's the same that we did on the 03:00.080 --> 03:00.530 stack. 03:00.590 --> 03:07.820 So it's gonna be queue the items it equals to append cue the items and the item. 03:07.820 --> 03:08.210 Right. 03:08.450 --> 03:12.860 So what we're doing here is we're pushing elements into our queue. 03:12.950 --> 03:19.160 So far the same the other stack here is different because rather than taking the newest element from 03:19.160 --> 03:23.200 the queue we need to take the first element. 03:23.210 --> 03:25.940 So in our slide is gonna be the first element. 03:26.120 --> 03:27.530 How do we do this. 03:27.590 --> 03:33.070 We will first take our item which is going to be equal to Q2 items. 03:33.590 --> 03:37.650 Sub zero which is the first one is always going to be the first one. 03:37.700 --> 03:40.630 Now we also need to modify the existing items. 03:40.680 --> 03:46.710 It is going to be queue of items and we want everything but the third one. 03:46.790 --> 03:55.040 So is starting from the index number one this is give me the rest of the items and now we will return 03:55.130 --> 04:01.000 item before that we need to assign the new items into the items from the queue. 04:01.010 --> 04:05.180 So it's gonna be queue that items equals two items. 04:05.180 --> 04:05.490 Right. 04:05.930 --> 04:09.420 Everything is fine let's execute this. 04:09.470 --> 04:09.850 OK. 04:10.080 --> 04:13.510 So looks like it's working. 04:13.530 --> 04:16.080 So what we're doing is pushing pushing pushing. 04:16.080 --> 04:21.930 Let me just push on other elements where you can make sure this is working and let's push number nine 04:22.290 --> 04:28.500 and everyday queue will take the elements the same or that I push them. 04:29.110 --> 04:35.490 This should be 1 9 2 3 and it's just 1 9 2 because I'm missing one here. 04:35.580 --> 04:41.870 So now I'm pushing four elements and I'm queuing for elements and if I do this simply work. 04:41.880 --> 04:49.400 Now let's try to grasp this by just pushing two elements 1 2 and the queuing for. 04:49.740 --> 04:55.940 So if I did queue for on a queue which with two elements something will happen right. 04:56.400 --> 04:57.200 Let's see what happens. 04:57.630 --> 05:05.390 And again slight bounce out of range because I'm trying to access properly index through an empty slice. 05:05.520 --> 05:08.230 So we all again need to check this. 05:08.300 --> 05:16.260 This is going to be if size of the queue is sero let's just return minus 1 which is exactly the same 05:16.260 --> 05:19.160 we did for the start. 05:19.430 --> 05:27.780 So here I'm trying to get the size I'm trying to get the size of my queue but it shouldn't be of the 05:27.930 --> 05:29.700 items of my queue. 05:29.750 --> 05:30.770 Instructor. 05:30.840 --> 05:37.020 I can get the size of the structure and I can get the size of the slicing inside the cube. 05:37.770 --> 05:41.030 So let's execute this again and it's fine. 05:41.040 --> 05:43.550 The last two are minus one because that's my rule. 05:43.560 --> 05:48.360 I'm saying if I don't have any other element on my queue is relevant as well. 05:48.870 --> 05:52.770 Now comes the exciting part which is implement these using channels. 05:52.770 --> 06:00.320 As I told you earlier channels is one of the novelty of Go language and it's a very powerful to be a 06:00.330 --> 06:01.700 concurrent application. 06:01.790 --> 06:08.010 Will explode that functionality later but I just want to make like a very brief introduction to them. 06:08.370 --> 06:16.170 So let me open a new file a B Q channels let go. 06:16.170 --> 06:24.770 So the first thing that I will do is again my package making my function and I'm gonna do this and I 06:24.780 --> 06:32.910 would have Q I guess it is going to be a channel into the Chinese have reserved Q for the language and 06:32.920 --> 06:36.030 then let's implement. 06:36.330 --> 06:43.450 Q Matthew he turned up to you and did you method. 06:43.890 --> 06:45.250 No argument. 06:45.390 --> 06:46.680 Return to Sender right. 06:48.320 --> 06:51.570 Zero waste no problem here. 06:51.650 --> 06:57.580 I need to know when you have more than one main function. 06:57.800 --> 06:58.600 It won't work. 06:59.540 --> 07:02.950 Because I have q with a main function in queue challenge with online function so I. 07:02.960 --> 07:04.460 This is just a trick. 07:04.460 --> 07:08.050 Don't pay attention to it to disable the automatic building for the editor. 07:08.160 --> 07:08.460 OK. 07:08.710 --> 07:09.020 Yeah. 07:09.140 --> 07:09.830 So this is fine. 07:10.610 --> 07:13.890 So let me open here. 07:13.940 --> 07:16.680 The previous implementation so we did. 07:16.680 --> 07:19.390 First we create a queue. 07:19.430 --> 07:22.340 The main function so I'm going to look kind of the same. 07:22.820 --> 07:23.400 OK. 07:23.530 --> 07:29.890 This is the queue and this is the difference for my items. 07:29.900 --> 07:36.980 I need to create a channel which is make channel type which is integer and then the size of those two 07:36.980 --> 07:38.090 kind of channels. 07:38.090 --> 07:46.930 We're not gonna leave early but let's say that our channel has capacity 16 which means that I can push 07:47.200 --> 07:50.380 up to 16 items before I get in the queue. 07:50.380 --> 07:52.750 One of those is the size of the queue right. 07:53.170 --> 07:55.360 So I have my queue here. 07:55.510 --> 08:00.700 I'm gonna basically copy this and it's going to be like this. 08:00.740 --> 08:11.570 I'm queuing 1 2 and I'm queuing 1 so for queuing is as simple as this queue of items row item which 08:11.570 --> 08:15.020 meant that item is being pushed to the queue. 08:15.260 --> 08:16.070 OK. 08:16.070 --> 08:20.890 And now the queuing is as simple as this. 08:20.940 --> 08:24.440 Return cue the icons. 08:24.530 --> 08:36.690 So if you take a look here pushing items into the direction of my cube which is pushing or queuing for 08:36.870 --> 08:43.110 cueing the arrow is before the child which is reading from the top. 08:43.470 --> 08:46.900 OK so let's just make sure this is working. 08:47.930 --> 08:49.440 And it isn't. 08:49.540 --> 08:52.400 Let's see why Oh I see. 08:52.440 --> 08:54.450 I'm defining a cue twice. 08:55.230 --> 08:57.210 OK here's the problem. 08:57.900 --> 08:58.470 Thank you. 08:58.860 --> 08:59.110 Yeah. 08:59.160 --> 09:00.450 I was coming you. 09:01.200 --> 09:01.650 Thank you. 09:01.950 --> 09:02.410 OK. 09:02.550 --> 09:07.400 So now I have my cue and may EU methods same as before. 09:07.540 --> 09:08.850 Like the interface is the same. 09:08.940 --> 09:15.780 I'm calling it the same way they tell me I'm not using the slices but sounds like execute this and it 09:15.780 --> 09:16.490 works. 09:16.490 --> 09:22.690 Now let me make you press again which is I'm gonna take an item from our channel which is empty. 09:22.820 --> 09:23.950 Sorry. 09:24.000 --> 09:25.180 Now it's gonna work. 09:25.240 --> 09:27.720 So it works. 09:27.780 --> 09:29.650 And now let's make it crust. 09:30.550 --> 09:35.750 And it has all the routine are sleep deadlock deadlock which means he knows that I'm trying to pull 09:35.750 --> 09:39.210 information from a channel we're just not gonna be reading it anymore. 09:39.260 --> 09:41.540 So it says there's a problem here. 09:41.630 --> 09:47.660 We're not gonna fix this problem now because remember this is just a very very fast introduction to 09:47.660 --> 09:48.380 the channel. 09:48.380 --> 09:56.960 The idea is basically to explain how to implement the cue and try to find the right approach to do it 09:57.080 --> 10:02.290 using idiomatic go tooling or structure which is channels. 10:02.450 --> 10:08.960 So these functions are going to be useful on the last section which is building concurrent application. 10:09.080 --> 10:10.170 So that's it for now. 10:10.450 --> 10:10.730 By.