WEBVTT

00:00.400 --> 00:01.240
Hi everyone.

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

00:02.080 --> 00:05.920
Now let's move on to the next gatherer which is scan.

00:06.320 --> 00:11.320
This builds on the idea of fold but adds an extra layer of usefulness.

00:11.520 --> 00:18.280
It's like fold, except that instead of producing one final result, it emits intermediate result after

00:18.280 --> 00:19.800
every element in the stream.

00:19.800 --> 00:24.920
So when using fold, you only get the final accumulated result at the end of the stream.

00:24.920 --> 00:31.200
That's what we saw in the previous lecture, but with scan, after every element is processed, it emits

00:31.200 --> 00:34.520
the current accumulated value downstream.

00:34.520 --> 00:39.440
That means if you are summing numbers, you will see the running total of each addition being emitted

00:39.440 --> 00:40.720
to the downstream.

00:41.080 --> 00:47.720
Since scan produces intermediate results, it's perfect for live tracking metrics like running a total

00:47.720 --> 00:53.320
of movie durations, a cumulative average of ratings, or a progress counter.

00:53.320 --> 00:58.520
For example, if you're analyzing a list of movies, scan can show you how the total duration grows

00:58.640 --> 01:00.760
as each movie is processed.

01:00.760 --> 01:05.000
So to summarize, you can think of scan as a fold with visibility.

01:05.040 --> 01:10.560
It performs the same accumulation logic, but continuously emits partial results.

01:10.560 --> 01:13.720
This makes it ideal for scenarios like dashboards.

01:13.820 --> 01:20.580
progress bar, or running analytics where you want to see every step of the aggregation in real time.

01:20.660 --> 01:24.660
So here is a code example of scan the syntax and everything.

01:24.660 --> 01:27.420
As you can see, this is very similar to fold.

01:27.460 --> 01:29.820
You have the supplier and the buy function.

01:29.820 --> 01:34.940
But the only thing that's different is that for every movie that's been passed from the stream, it's

01:34.940 --> 01:39.380
going to be emitting the running total and you'll be able to see it in the console.

01:39.380 --> 01:44.500
So this helps you visualize how the total evolves as we stream through the data.

01:44.540 --> 01:47.260
Let's go back to the code and start exploring this in action.

01:47.300 --> 01:48.500
Let's go back to the code.

01:48.820 --> 01:50.340
And here I'm back in IntelliJ.

01:50.540 --> 01:55.940
So I'm going to be commenting out the fold operation so that our console looks cleaner.

01:56.140 --> 01:57.380
Let's go to the scan.

01:57.420 --> 02:02.780
Actually we can copy the same code that we wrote for the duration and then put it over here.

02:02.780 --> 02:06.020
So in the place of fold all you got to do is scan.

02:06.180 --> 02:10.540
But instead of collecting the result, what I'm going to do I'm going to be doing a for each.

02:10.940 --> 02:15.460
And then I'm going to give a running total and then print that running total in the console.

02:15.500 --> 02:15.780
Okay.

02:15.820 --> 02:17.580
So we don't need this logger anymore.

02:17.700 --> 02:18.300
There you go.

02:18.340 --> 02:19.900
It's not going to return anything.

02:20.060 --> 02:20.660
Take that off.

02:20.660 --> 02:21.300
So there you go.

02:21.340 --> 02:24.300
We have the scan ready actually.

02:24.340 --> 02:27.520
So let's go ahead and execute this so that you'll be able to see the result on the.

02:33.880 --> 02:34.520
Screen here.

02:34.520 --> 02:39.640
As you can see, for each and every movie, you will be able to see the value being growing little by

02:39.640 --> 02:39.920
little.

02:39.920 --> 02:41.720
So first one is 175.

02:41.920 --> 02:51.760
That's our first movie, and 175 plus 142 is 317 and 317 plus 154 is basically 471.

02:51.760 --> 02:58.840
And this is the beauty of scan where it's going to continuously emit as the gatherer is performing the

02:58.880 --> 03:00.640
summation in our use case.

03:00.640 --> 03:06.360
So it's the same concept for, uh, average ratings and the same concept for titles.

03:06.400 --> 03:08.480
Actually, we can actually explore one of them.

03:08.480 --> 03:11.600
Let's go here copy this and then put it over here.

03:11.600 --> 03:13.760
So in the place of this I'm going to do a scan.

03:14.000 --> 03:15.840
Take this off take this off.

03:15.840 --> 03:17.640
And then we can do for each.

03:17.680 --> 03:20.120
We can actually print the string over here okay.

03:20.160 --> 03:24.400
Let's run this program one more time so you can see that the title is growing one by one.

03:24.400 --> 03:29.840
It started with Godfather and then it added the next movie, it added the next movie, and the list

03:29.840 --> 03:30.840
goes on and on.

03:30.840 --> 03:32.400
So this is the beauty of scan.

03:32.440 --> 03:36.760
You have the ability to see where the accumulation is for each and every element.

03:36.760 --> 03:38.200
That's part of the stream.

03:38.200 --> 03:39.680
This marks the end of this lecture.

03:39.680 --> 03:40.680
Thank you for watching.
