WEBVTT

00:01.080 --> 00:01.800
Hi everyone.

00:01.800 --> 00:02.560
Welcome back.

00:02.600 --> 00:07.840
Let's now explore one of the most powerful built in gatherers, which is fold.

00:07.880 --> 00:14.240
We use it to combine or accumulate elements in a stream while maintaining an internal state.

00:14.240 --> 00:20.040
So when we say stateful reduction, it means the fold operation keeps track of running state.

00:20.160 --> 00:26.040
For example, some count or concatenation as elements pass through the stream.

00:26.160 --> 00:32.720
This is especially useful when you want to compute a single result, like a total duration of all the

00:32.720 --> 00:33.840
movies in the stream.

00:34.000 --> 00:38.360
Our total cost or even build a string from multiple elements.

00:38.520 --> 00:45.360
If you have used the reduce function that's part of the streams before, this concept will feel similar,

00:45.360 --> 00:46.960
but there is a key difference.

00:46.960 --> 00:53.080
Reduce is a terminal operation, meaning once you call it, your stream ends and you get one final result.

00:53.280 --> 00:58.960
But fold is an intermediate operation, meaning it happens inside the stream pipeline.

00:59.080 --> 01:03.440
You can actually continue transforming or filtering forward.

01:03.480 --> 01:05.360
Now let's break down the syntax.

01:05.360 --> 01:09.320
The syntax for fold takes in a supplier and a buy function.

01:09.360 --> 01:15.880
The supplier provides the initial state like zero for summing or an empty string for concatenation and

01:15.880 --> 01:20.320
buy function defines how each element should update the state.

01:20.320 --> 01:21.200
For instance.

01:21.240 --> 01:22.960
Accumulation plus value.

01:23.000 --> 01:29.040
The simple structure gives you complete control over how data is accumulated while the stream flows.

01:29.120 --> 01:32.040
So in this case here we have the code example.

01:32.040 --> 01:36.360
In this example we are calculating the total duration of all the movies.

01:36.360 --> 01:38.640
So in this case we use a gather.

01:38.640 --> 01:41.680
So the movie is going to be the collection.

01:41.880 --> 01:44.960
And we are gathering all the movies in the collection.

01:44.960 --> 01:49.960
So we have the initial state which is zero over here, and then the actual movie.

01:49.960 --> 01:56.120
So what we are doing here is that when the first element is processed, we process the zero plus the

01:56.120 --> 01:57.080
movie duration.

01:57.080 --> 02:02.670
But after that we are going to be continuously getting all the movies and adding the duration to it,

02:02.710 --> 02:06.030
and then we call the find first to get the final result.

02:06.230 --> 02:11.070
If there are no movies in the movies collection, then we will get the value as zero.

02:11.230 --> 02:14.430
So this is how we are gathering the duration.

02:14.430 --> 02:19.950
Basically, in this case, we are performing a stateful operation of maintaining the state of what the

02:19.950 --> 02:20.990
duration was.

02:21.190 --> 02:26.510
When each and every movie is being gathered by this gather operator.

02:26.550 --> 02:27.750
I hope the explanation is clear.

02:27.750 --> 02:31.070
Let's go ahead and explore this in action by getting back to IntelliJ.

02:31.110 --> 02:32.630
So I'm back in IntelliJ now.

02:32.790 --> 02:36.150
So in this case we are going to be exploring the fold operation.

02:36.150 --> 02:41.710
So let's go ahead and comment out this window sliding so that our console looks cleaner.

02:41.710 --> 02:43.510
So let's go to the demonstrate fold.

02:43.510 --> 02:45.230
So in here what I'm going to do.

02:45.230 --> 02:47.790
So I'm going to be performing the same operation.

02:47.790 --> 02:50.350
So in this case movies dot stream.

02:50.510 --> 02:53.870
And we are going to be calling the gather operator here.

02:53.870 --> 02:56.830
So gather dot fold and the fold.

02:56.830 --> 02:58.550
You can see it takes in a supplier.

02:58.550 --> 03:02.430
So the very first thing is we basically provide our initial state.

03:02.430 --> 03:04.430
The initial duration is going to be zero.

03:04.590 --> 03:07.550
And after that we can provide the bi function.

03:07.550 --> 03:10.270
So the bi function takes in two input.

03:10.270 --> 03:14.230
And then it's going to be giving you an output which is in this case is integer.

03:14.230 --> 03:19.630
So in this case now we are going to be calling the find first this is going to be an optional integer.

03:19.830 --> 03:25.430
If it's not present then we are going to be calling or else the value is going to be zero okay there

03:25.430 --> 03:25.870
we go.

03:26.030 --> 03:26.430
There we go.

03:26.430 --> 03:29.070
So in this case we are doing the stream of movie.

03:29.350 --> 03:32.310
And here this is going to be maintaining the state.

03:32.310 --> 03:37.590
So this particular operation is the one which is going to stream through all the movies and then compute

03:37.590 --> 03:41.070
the duration of all the movies by running this logic.

03:41.390 --> 03:47.110
Once that is done, it's going to be calling the find first, and then it's going to return the total

03:47.110 --> 03:47.630
duration.

03:47.630 --> 03:50.350
So I'm going to be assigning this to a variable.

03:50.350 --> 03:52.910
So I'm going to name this one as total duration.

03:52.950 --> 03:53.590
There you go.

03:53.590 --> 03:56.590
So this is going to give you the duration over there.

03:56.710 --> 03:59.310
Now I'm going to be printing the value.

03:59.350 --> 04:03.390
So in this case, total duration of all the movies is this minutes.

04:03.550 --> 04:04.270
So this is good.

04:04.270 --> 04:06.150
Let's go ahead and execute the program.

04:06.150 --> 04:08.950
Let's go to the top and let's execute this program.

04:08.950 --> 04:14.270
So here you can see that the total duration of all the minutes is 173 two minutes.

04:14.430 --> 04:19.510
So you can actually do a total of all these things that will give you this result.

04:19.550 --> 04:19.910
Okay.

04:19.950 --> 04:23.790
Now let's explore some other example in the same fold operation.

04:23.790 --> 04:24.910
Let's go to this one.

04:24.910 --> 04:29.510
So what I'm going to do is I'm going to be calculating the average rating okay.

04:29.550 --> 04:31.990
So I'm going to pretty much copy the similar code.

04:32.030 --> 04:34.190
But I'm going to change things a little bit.

04:34.190 --> 04:38.870
So in this case what we are going to calculate is a average rating.

04:38.870 --> 04:44.510
And in here instead of duration what we are going to do is we are going to be calculating the rating

04:44.510 --> 04:45.750
over here okay.

04:45.790 --> 04:47.830
And this is going to be of type double.

04:47.830 --> 04:49.270
That's why I see this error.

04:49.270 --> 04:51.110
So because rating is of type double.

04:51.110 --> 04:55.230
So in this case we can change the supplier to represent a double.

04:55.230 --> 04:55.870
There we go.

04:56.260 --> 04:58.340
And this is also going to be a double.

04:58.340 --> 05:01.540
So what this is going to do this is going to give you an average rating.

05:01.540 --> 05:01.900
Right.

05:01.900 --> 05:04.580
This is going to give you an addition of all the ratings.

05:04.580 --> 05:05.660
So what do we need to do.

05:05.660 --> 05:09.420
So I'm going to do a division of movie dot size.

05:09.420 --> 05:14.740
So this is going to give you the average rating of all the movies that we have in our collection.

05:14.740 --> 05:16.620
So let's go ahead and print this value also.

05:16.620 --> 05:20.180
So average rating of all the movies is string dot format.

05:20.180 --> 05:22.740
And this is the average rating okay.

05:22.820 --> 05:27.260
So now the next use case we can code multiple use cases based on the fold operation.

05:27.260 --> 05:31.020
So now I want to just concatenate all the titles okay.

05:31.060 --> 05:33.660
So what I'm going to do I'm going to do something similar.

05:33.860 --> 05:37.140
But in this case it's not going to be the rating here.

05:37.140 --> 05:39.660
It's going to be the movie title okay.

05:39.700 --> 05:41.300
So in this case movie title.

05:41.500 --> 05:45.300
And this is going to be an empty string as a initial state okay.

05:45.460 --> 05:49.540
So I'm going to change this one to a variable with the name accumulator.

05:49.540 --> 05:50.580
So accumulator.

05:50.580 --> 05:52.900
So there is a little logic I need to write actually.

05:52.900 --> 05:55.100
So if the value is empty.

05:55.260 --> 05:59.540
If the accumulator value is empty, which is going to be empty for the very first time, right?

05:59.580 --> 06:02.700
If it's empty, I'm going to be adding the movie title.

06:02.700 --> 06:08.220
Otherwise I'm going to be adding the accumulator plus the movie title.

06:08.260 --> 06:08.460
Okay.

06:08.540 --> 06:13.300
So this is going to give me the movie titles as comma separated values.

06:13.300 --> 06:17.220
So in this case this is also going to be empty string.

06:17.220 --> 06:19.820
We don't want the division over there.

06:19.820 --> 06:22.580
So this is going to be all titles okay.

06:22.620 --> 06:23.900
All titles.

06:23.900 --> 06:29.180
And another thing about our copilot is that let's say this code logic is very complex for you to understand.

06:29.180 --> 06:34.660
So what I'm going to do is I'm going to highlight this code and this particular inline chat will open.

06:34.700 --> 06:35.820
So let's open that one.

06:35.860 --> 06:38.540
And then you can ask explain this code to me.

06:38.580 --> 06:38.780
Right.

06:38.780 --> 06:43.540
When you do that it's going to give you an explanation of what that logic is so that you will be able

06:43.540 --> 06:45.940
to understand what it actually does.

06:45.940 --> 06:46.780
So here.

06:49.860 --> 06:50.300
Okay.

06:50.340 --> 06:55.820
So here if you take a look at it, the fold operations collapses the entire stream of movies into one

06:55.820 --> 06:56.140
result.

06:56.180 --> 06:59.260
By this is the initial value, which is the empty string.

06:59.260 --> 07:02.860
And then here we are actually performing the accumulator.

07:02.860 --> 07:05.180
So bills a comma separated list.

07:05.180 --> 07:10.980
So the terminal operation produces exactly one output regardless of the input size.

07:10.980 --> 07:14.300
It maintains the accumulated straight across this elements.

07:14.300 --> 07:19.380
And in this case if you take a look at it let's say if you have three movies it transforms it to this

07:19.380 --> 07:21.500
particular single string value.

07:21.620 --> 07:25.620
This is different from scan which would emit intermediate results.

07:25.620 --> 07:28.500
So here there is no emission of intermediate result.

07:28.780 --> 07:34.300
It folds the overall list of elements into one single output and then emits that value.

07:34.340 --> 07:34.780
Okay.

07:34.860 --> 07:39.740
So what we have done is we have coded the average rating and all titles.

07:39.740 --> 07:41.780
Let me go ahead and print that one also.

07:41.780 --> 07:44.780
So in this case over there all titles okay.

07:44.820 --> 07:47.700
So now let's go ahead and execute this particular function.

07:47.700 --> 07:52.860
This is going to print all the three functionalities that we have coded using the fold operator.

07:52.890 --> 07:59.970
So in this case, the total duration of all the movies is this, and the average rating is 8.82 stars

07:59.970 --> 08:04.290
and all movie titles, you can see that it's all comma separated nicely.

08:04.290 --> 08:07.490
So if you go to the top, let me explore the code one more time.

08:07.490 --> 08:08.970
Let's go to the fold operation.

08:08.970 --> 08:15.530
So this is a total duration of all the movies where we are simply performing summation of all the durations.

08:15.530 --> 08:22.770
So here we are also performing the same summation concept, but we divided the whole result by the movies

08:22.770 --> 08:25.290
dot size to get the average rating.

08:25.330 --> 08:26.490
And then if you go here.

08:26.650 --> 08:33.530
So here we are performing the string concatenation operation where we are taking each title and then

08:33.570 --> 08:35.810
having them separated by comma.

08:36.010 --> 08:37.130
That's what this one is.

08:37.370 --> 08:40.810
So this is where the folding operation is really handy.

08:41.090 --> 08:47.850
If you want to perform an intermediate operation with state and then produce a final result, then fold

08:47.850 --> 08:49.530
is an excellent option for that.

08:49.570 --> 08:50.970
This marks the end of this lecture.

08:50.970 --> 08:51.970
Thank you for watching.
