WEBVTT

00:00.120 --> 00:00.680
Hi everyone.

00:00.680 --> 00:01.240
Welcome back.

00:01.240 --> 00:06.160
In this lecture we are going to be learning how to build our custom gatherers.

00:06.160 --> 00:12.720
So far we have explored all the different gatherers that are part of the API itself, which is basically

00:12.720 --> 00:14.040
part of the language itself.

00:14.040 --> 00:17.240
Now we are going to build something on our own for that purpose.

00:17.240 --> 00:22.360
I have a specific function called Demonstrate simple custom gatherer.

00:22.600 --> 00:24.000
So let's go to that function.

00:24.000 --> 00:26.480
So in this function we have this implementation.

00:26.480 --> 00:30.360
Currently the implementation here is that we are filtering the movies.

00:30.520 --> 00:36.640
And then we are transforming the movie to be in this format which is we have the star emoji over here.

00:36.640 --> 00:39.800
And then we are formatting using the title release.

00:39.840 --> 00:44.120
Here we are basically displaying the data in this particular format.

00:44.120 --> 00:46.560
And then we are printing the summary over here.

00:46.560 --> 00:52.400
So we are filtering the movies that are greater than 8.5 in rating and then printing the summary.

00:52.440 --> 00:55.280
Let's quickly execute this and then show you what the result is.

00:55.520 --> 01:01.640
And then we will see how we can implement the same logic using a custom gatherer implementation.

01:01.640 --> 01:01.710
Rotation.

01:02.990 --> 01:05.590
So this is how the result is going to be.

01:05.590 --> 01:08.350
So what we have is a traditional approach.

01:08.510 --> 01:13.110
Now we are going to be implementing the same logic using a custom gatherer.

01:13.110 --> 01:16.070
But as I mentioned before you have the prompt here.

01:16.070 --> 01:20.510
If you want to use prompt, please go ahead and use the prompt to write the code for you.

01:20.510 --> 01:26.270
But in this case, I'm going to be coding this by hand so that you are able to grasp the concept as

01:26.270 --> 01:26.910
we code.

01:26.990 --> 01:31.270
Okay, so in this case I'm going to call gatherer dot off.

01:31.590 --> 01:33.430
So you have multiple options off and off.

01:33.430 --> 01:36.070
Sequential off is what we are going to use for now.

01:36.230 --> 01:39.910
Off sequential is something which we will look at in the next lecture.

01:39.910 --> 01:41.950
So user off over here.

01:42.350 --> 01:43.470
What do we need actually.

01:43.470 --> 01:48.150
So if you go and look at the implementation of off so you have many different implementations.

01:48.150 --> 01:52.910
You have the integrator as like the first two options and then you have the supplier.

01:52.910 --> 01:54.630
But our use case is very simple.

01:54.630 --> 01:57.310
We don't want to maintain any state or anything like that.

01:57.350 --> 02:03.550
What we need is a transformer that's going to be taking the data, apply the filtering logic which is

02:03.550 --> 02:07.310
over here, and the map logic, and then push the value downstream.

02:07.310 --> 02:12.430
So what I'm going to do, I'm going to call something called gatherer dot integrator.

02:12.470 --> 02:14.030
So you can call an integrator.

02:14.030 --> 02:16.630
So integrator is the one which does the work for us.

02:16.630 --> 02:22.190
That's the one which is going to be taking care of applying the filtering logic and also the transforming

02:22.190 --> 02:22.630
logic.

02:22.630 --> 02:24.590
And then you have multiple options over here.

02:24.590 --> 02:30.110
You have the off greedy and off off greedy is something which is going to consume all the elements.

02:30.110 --> 02:30.910
That's part of this.

02:31.110 --> 02:33.790
So in our case we have a collection of movies, right.

02:33.790 --> 02:35.590
We want to consume all the elements.

02:35.590 --> 02:41.430
So in those kind of scenarios you would use off greedy off is something which we'll use in the integrator.

02:41.430 --> 02:45.870
If you want to apply some conditional logic which is not needed for our use case.

02:46.110 --> 02:46.550
Okay.

02:46.590 --> 02:48.470
And then I'm going to be adding a lambda.

02:48.710 --> 02:52.390
So this lambda is going to be holding a state.

02:52.390 --> 02:55.350
And then the movie and then the downstream.

02:55.350 --> 03:01.310
So downstream is how you basically push the value downstream in our code logic okay.

03:01.350 --> 03:03.430
So now the code logic is this one.

03:03.430 --> 03:10.060
If the movie Movieid rating is greater than 8.5, then take those movies and apply this filtering logic.

03:10.140 --> 03:13.380
So in here we need to basically give it a type actually.

03:13.380 --> 03:17.540
Otherwise it will complain about a movie is basically of type object.

03:17.540 --> 03:19.540
It cannot read this rating value.

03:19.540 --> 03:21.180
So in this case what we are going to do.

03:21.420 --> 03:23.700
So we are going to be calling gatherer.

03:23.740 --> 03:26.100
We need to define the types and everything over here.

03:26.140 --> 03:26.580
Okay.

03:26.660 --> 03:31.060
Then I'm going to give it a name called High Rated Movie.

03:31.100 --> 03:32.500
Summary okay.

03:32.540 --> 03:35.060
So this gatherer is going to be of type movie.

03:35.180 --> 03:39.620
And then the next step is going to be void because we are not going to maintain any state.

03:39.620 --> 03:41.180
The next one is going to be the output.

03:41.220 --> 03:42.540
What is the output we want.

03:42.660 --> 03:44.620
The output is going to be of type string.

03:44.660 --> 03:44.900
Right.

03:44.940 --> 03:47.460
So in this case let's provide the value as string okay.

03:47.500 --> 03:51.860
So as soon as I provided these three type now the compilation issue is gone.

03:51.860 --> 03:53.300
So the movie is a input.

03:53.300 --> 03:58.660
So if you go and take a look at the gatherer implementation here, it has a input type and output type

03:58.780 --> 04:05.460
and the actual state which is the one that you might need if you want to manage some State.

04:05.460 --> 04:08.220
We will look at it in the next lecture, but for now, let's keep it simple.

04:08.420 --> 04:11.580
So the input is movie and the output is string.

04:11.820 --> 04:12.260
Okay.

04:12.500 --> 04:17.700
And another thing that we need to do here is that after the downstream dot push is over, I think we

04:17.700 --> 04:19.420
have one another extra colon.

04:19.460 --> 04:22.460
Oh one thing we need to do is we need to return the value equal to true.

04:22.580 --> 04:26.380
So this means we are going to be continuing the processing.

04:26.380 --> 04:28.620
So that's what that one is meaning actually.

04:28.620 --> 04:30.620
And then add a semicolon over here.

04:30.620 --> 04:32.580
So this is our custom gatherer.

04:32.620 --> 04:36.660
This implementation is very similar to adding the filter and map.

04:36.780 --> 04:42.580
I do understand that this might look a little complex compared to this filter and map, which is very

04:42.580 --> 04:44.940
intuitive, and we are very much used to it.

04:45.100 --> 04:51.060
I'm just showing an option of how you can leverage gatherer for custom implementation, but if you think

04:51.100 --> 04:56.180
a traditional way of doing it using the filter and map and easy, please don't hesitate to use it.

04:56.180 --> 04:59.300
There is no need for you to use gatherer.

04:59.540 --> 05:05.620
If you think the logic is simple with filter and map intermediate operations, that is something I want

05:05.660 --> 05:06.490
to call it out.

05:06.490 --> 05:08.410
So now we have this gatherer created.

05:08.410 --> 05:14.250
Now what we are going to do here is that we are going to be using the movies dot stream, and then we

05:14.290 --> 05:19.970
are going to be passing the gatherer, which is this gatherer, and we are going to be printing the

05:19.970 --> 05:21.130
result in the console.

05:21.170 --> 05:21.650
Okay.

05:21.810 --> 05:25.850
So now let's go ahead and execute the program and see what the result is.

05:25.890 --> 05:29.130
So it's going to be rerunning the program one more time.

05:29.130 --> 05:31.650
You will see the same result being printed over here.

05:31.690 --> 05:32.170
There you go.

05:32.170 --> 05:33.770
So here is a traditional approach.

05:33.970 --> 05:37.210
And here is the actual gatherer approach.

05:37.210 --> 05:40.610
So now let's say if you don't push this what happens.

05:40.650 --> 05:40.810
Right.

05:40.810 --> 05:41.930
I'm not pushing anything.

05:41.930 --> 05:43.490
Ideally there should not be any data.

05:43.530 --> 05:43.970
Right.

05:43.970 --> 05:45.930
Let's go ahead and run this program one more time.

05:45.930 --> 05:47.690
You see there are no result.

05:47.690 --> 05:55.690
So downstream dot push is one of the code which is responsible for actually push the gathered result

05:55.690 --> 05:58.050
to the for each terminal operation.

05:58.090 --> 05:58.330
Okay.

05:58.370 --> 05:59.530
So this is really key.

05:59.650 --> 06:05.490
So so far the reason why we have been seeing the code working as expected for all the different gatherers

06:05.490 --> 06:06.450
that we have used.

06:06.570 --> 06:10.210
If you go ahead and take a look at the windows fixed logic.

06:10.250 --> 06:13.930
You will have a gatherer down the line that takes care of pushing the data.

06:13.970 --> 06:14.730
You can see that right?

06:14.770 --> 06:18.650
So here also we use the integrator dot off greedy.

06:18.690 --> 06:21.210
That's exactly what we are doing in our code also.

06:21.290 --> 06:26.410
So in our code also what we used is gatherer dot integrator dot off greedy.

06:26.690 --> 06:32.930
So if you don't use downstream dot push then no data will be pushed to the final result.

06:32.970 --> 06:36.930
Let me execute this program one more time and then show you the difference.

06:37.130 --> 06:37.530
There you go.

06:37.530 --> 06:38.650
Now we see the result.

06:38.690 --> 06:44.690
I hope you all were able to successfully implement a custom gatherer based on your traditional way of

06:44.730 --> 06:46.530
doing the filtering and mapping.

06:46.530 --> 06:51.010
So here we achieved both filtering and mapping within the gatherer itself.

06:51.050 --> 06:57.530
Again there is no hard rule of you need to use gatherer replacing all the filter map with gatherer.

06:57.530 --> 07:00.970
So gatherer is something which is handy for specific use cases.

07:00.970 --> 07:06.250
If you think if your use case can benefit from it, please go ahead and start using gatherer.

07:06.290 --> 07:07.890
But this marks the end of this lecture.

07:07.890 --> 07:08.890
Thank you for watching.
