Hi in this episode we will learn what IQ is and how to implement it.
Also after our implementation is done we will reduce it using channels.
A very powerful idea that will help us build concurrent applications.
So IQ is very similar to this type of language learned that on the previous episode it's basically our
data abstractions which allow us to process data so the general idea of a queue is that I in queue items
into the queue and I did queue items from the front of the queue.
So it is our queue will in queue which is analogous to push from the start that would learn on the previous
episode.
So in this case we have a number of one in the queue.
We push them to put number three four five six.
But rather than popping items which will be six five four three and two we did queue them which is taking
the oldest item first.
In this case two three four five and six.
OK.
So let's review the previous stack which is like this.
You remember you push items which will be analogous to in queue rather than shopping which is taking
the newest with the queue then just taking the orders.
OK.
So let's apply our implementation of this.
So very pretty similar.
So I wanted to first is to define our queue structure which will have again items and items is going
to be a slice of integers.
Remember you can choose any time you want and we know that we need to do.
Thank you.
These will receive an I and II type and I'm going to return anything.
And we also need the queue method.
Sorry.
The queue here which is not receiving anything and is returning on into it right.
Let me think those errors this queue is capital queue.
OK.
So what we know is that we will be a new queue here and we can call in queue 1 in Q2.
And let's say 3.
And then when we do queue them we will need to get the elements in the same order that we queue them
right.
So these like a queue of a bank the first that comes to the queue is the first to get serviced.
Let me just run this to make sure nothing is broken yet and it's fine.
We're just returning 0 because our implementation sense here that the queue will just reach and zero
and a queue does.
Basically I'm not just an empty implementation.
So let's start by implementing and queuing and this is really easy it's the same that we did on the
stack.
So it's gonna be queue the items it equals to append cue the items and the item.
Right.
So what we're doing here is we're pushing elements into our queue.
So far the same the other stack here is different because rather than taking the newest element from
the queue we need to take the first element.
So in our slide is gonna be the first element.
How do we do this.
We will first take our item which is going to be equal to Q2 items.
Sub zero which is the first one is always going to be the first one.
Now we also need to modify the existing items.
It is going to be queue of items and we want everything but the third one.
So is starting from the index number one this is give me the rest of the items and now we will return
item before that we need to assign the new items into the items from the queue.
So it's gonna be queue that items equals two items.
Right.
Everything is fine let's execute this.
OK.
So looks like it's working.
So what we're doing is pushing pushing pushing.
Let me just push on other elements where you can make sure this is working and let's push number nine
and everyday queue will take the elements the same or that I push them.
This should be 1 9 2 3 and it's just 1 9 2 because I'm missing one here.
So now I'm pushing four elements and I'm queuing for elements and if I do this simply work.
Now let's try to grasp this by just pushing two elements 1 2 and the queuing for.
So if I did queue for on a queue which with two elements something will happen right.
Let's see what happens.
And again slight bounce out of range because I'm trying to access properly index through an empty slice.
So we all again need to check this.
This is going to be if size of the queue is sero let's just return minus 1 which is exactly the same
we did for the start.
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
items of my queue.
Instructor.
I can get the size of the structure and I can get the size of the slicing inside the cube.
So let's execute this again and it's fine.
The last two are minus one because that's my rule.
I'm saying if I don't have any other element on my queue is relevant as well.
Now comes the exciting part which is implement these using channels.
As I told you earlier channels is one of the novelty of Go language and it's a very powerful to be a
concurrent application.
Will explode that functionality later but I just want to make like a very brief introduction to them.
So let me open a new file a B Q channels let go.
So the first thing that I will do is again my package making my function and I'm gonna do this and I
would have Q I guess it is going to be a channel into the Chinese have reserved Q for the language and
then let's implement.
Q Matthew he turned up to you and did you method.
No argument.
Return to Sender right.
Zero waste no problem here.
I need to know when you have more than one main function.
It won't work.
Because I have q with a main function in queue challenge with online function so I.
This is just a trick.
Don't pay attention to it to disable the automatic building for the editor.
OK.
Yeah.
So this is fine.
So let me open here.
The previous implementation so we did.
First we create a queue.
The main function so I'm going to look kind of the same.
OK.
This is the queue and this is the difference for my items.
I need to create a channel which is make channel type which is integer and then the size of those two
kind of channels.
We're not gonna leave early but let's say that our channel has capacity 16 which means that I can push
up to 16 items before I get in the queue.
One of those is the size of the queue right.
So I have my queue here.
I'm gonna basically copy this and it's going to be like this.
I'm queuing 1 2 and I'm queuing 1 so for queuing is as simple as this queue of items row item which
meant that item is being pushed to the queue.
OK.
And now the queuing is as simple as this.
Return cue the icons.
So if you take a look here pushing items into the direction of my cube which is pushing or queuing for
cueing the arrow is before the child which is reading from the top.
OK so let's just make sure this is working.
And it isn't.
Let's see why Oh I see.
I'm defining a cue twice.
OK here's the problem.
Thank you.
Yeah.
I was coming you.
Thank you.
OK.
So now I have my cue and may EU methods same as before.
Like the interface is the same.
I'm calling it the same way they tell me I'm not using the slices but sounds like execute this and it
works.
Now let me make you press again which is I'm gonna take an item from our channel which is empty.
Sorry.
Now it's gonna work.
So it works.
And now let's make it crust.
And it has all the routine are sleep deadlock deadlock which means he knows that I'm trying to pull
information from a channel we're just not gonna be reading it anymore.
So it says there's a problem here.
We're not gonna fix this problem now because remember this is just a very very fast introduction to
the channel.
The idea is basically to explain how to implement the cue and try to find the right approach to do it
using idiomatic go tooling or structure which is channels.
So these functions are going to be useful on the last section which is building concurrent application.
So that's it for now.
By.