Hi everyone.
Welcome back.
In this lecture we are going to be learning how to build our custom gatherers.
So far we have explored all the different gatherers that are part of the API itself, which is basically
part of the language itself.
Now we are going to build something on our own for that purpose.
I have a specific function called Demonstrate simple custom gatherer.
So let's go to that function.
So in this function we have this implementation.
Currently the implementation here is that we are filtering the movies.
And then we are transforming the movie to be in this format which is we have the star emoji over here.
And then we are formatting using the title release.
Here we are basically displaying the data in this particular format.
And then we are printing the summary over here.
So we are filtering the movies that are greater than 8.5 in rating and then printing the summary.
Let's quickly execute this and then show you what the result is.
And then we will see how we can implement the same logic using a custom gatherer implementation.
Rotation.
So this is how the result is going to be.
So what we have is a traditional approach.
Now we are going to be implementing the same logic using a custom gatherer.
But as I mentioned before you have the prompt here.
If you want to use prompt, please go ahead and use the prompt to write the code for you.
But in this case, I'm going to be coding this by hand so that you are able to grasp the concept as
we code.
Okay, so in this case I'm going to call gatherer dot off.
So you have multiple options off and off.
Sequential off is what we are going to use for now.
Off sequential is something which we will look at in the next lecture.
So user off over here.
What do we need actually.
So if you go and look at the implementation of off so you have many different implementations.
You have the integrator as like the first two options and then you have the supplier.
But our use case is very simple.
We don't want to maintain any state or anything like that.
What we need is a transformer that's going to be taking the data, apply the filtering logic which is
over here, and the map logic, and then push the value downstream.
So what I'm going to do, I'm going to call something called gatherer dot integrator.
So you can call an integrator.
So integrator is the one which does the work for us.
That's the one which is going to be taking care of applying the filtering logic and also the transforming
logic.
And then you have multiple options over here.
You have the off greedy and off off greedy is something which is going to consume all the elements.
That's part of this.
So in our case we have a collection of movies, right.
We want to consume all the elements.
So in those kind of scenarios you would use off greedy off is something which we'll use in the integrator.
If you want to apply some conditional logic which is not needed for our use case.
Okay.
And then I'm going to be adding a lambda.
So this lambda is going to be holding a state.
And then the movie and then the downstream.
So downstream is how you basically push the value downstream in our code logic okay.
So now the code logic is this one.
If the movie Movieid rating is greater than 8.5, then take those movies and apply this filtering logic.
So in here we need to basically give it a type actually.
Otherwise it will complain about a movie is basically of type object.
It cannot read this rating value.
So in this case what we are going to do.
So we are going to be calling gatherer.
We need to define the types and everything over here.
Okay.
Then I'm going to give it a name called High Rated Movie.
Summary okay.
So this gatherer is going to be of type movie.
And then the next step is going to be void because we are not going to maintain any state.
The next one is going to be the output.
What is the output we want.
The output is going to be of type string.
Right.
So in this case let's provide the value as string okay.
So as soon as I provided these three type now the compilation issue is gone.
So the movie is a input.
So if you go and take a look at the gatherer implementation here, it has a input type and output type
and the actual state which is the one that you might need if you want to manage some State.
We will look at it in the next lecture, but for now, let's keep it simple.
So the input is movie and the output is string.
Okay.
And another thing that we need to do here is that after the downstream dot push is over, I think we
have one another extra colon.
Oh one thing we need to do is we need to return the value equal to true.
So this means we are going to be continuing the processing.
So that's what that one is meaning actually.
And then add a semicolon over here.
So this is our custom gatherer.
This implementation is very similar to adding the filter and map.
I do understand that this might look a little complex compared to this filter and map, which is very
intuitive, and we are very much used to it.
I'm just showing an option of how you can leverage gatherer for custom implementation, but if you think
a traditional way of doing it using the filter and map and easy, please don't hesitate to use it.
There is no need for you to use gatherer.
If you think the logic is simple with filter and map intermediate operations, that is something I want
to call it out.
So now we have this gatherer created.
Now what we are going to do here is that we are going to be using the movies dot stream, and then we
are going to be passing the gatherer, which is this gatherer, and we are going to be printing the
result in the console.
Okay.
So now let's go ahead and execute the program and see what the result is.
So it's going to be rerunning the program one more time.
You will see the same result being printed over here.
There you go.
So here is a traditional approach.
And here is the actual gatherer approach.
So now let's say if you don't push this what happens.
Right.
I'm not pushing anything.
Ideally there should not be any data.
Right.
Let's go ahead and run this program one more time.
You see there are no result.
So downstream dot push is one of the code which is responsible for actually push the gathered result
to the for each terminal operation.
Okay.
So this is really key.
So so far the reason why we have been seeing the code working as expected for all the different gatherers
that we have used.
If you go ahead and take a look at the windows fixed logic.
You will have a gatherer down the line that takes care of pushing the data.
You can see that right?
So here also we use the integrator dot off greedy.
That's exactly what we are doing in our code also.
So in our code also what we used is gatherer dot integrator dot off greedy.
So if you don't use downstream dot push then no data will be pushed to the final result.
Let me execute this program one more time and then show you the difference.
There you go.
Now we see the result.
I hope you all were able to successfully implement a custom gatherer based on your traditional way of
doing the filtering and mapping.
So here we achieved both filtering and mapping within the gatherer itself.
Again there is no hard rule of you need to use gatherer replacing all the filter map with gatherer.
So gatherer is something which is handy for specific use cases.
If you think if your use case can benefit from it, please go ahead and start using gatherer.
But this marks the end of this lecture.
Thank you for watching.