Hi everyone.
Welcome back.
Now let's move on to the next gatherer which is scan.
This builds on the idea of fold but adds an extra layer of usefulness.
It's like fold, except that instead of producing one final result, it emits intermediate result after
every element in the stream.
So when using fold, you only get the final accumulated result at the end of the stream.
That's what we saw in the previous lecture, but with scan, after every element is processed, it emits
the current accumulated value downstream.
That means if you are summing numbers, you will see the running total of each addition being emitted
to the downstream.
Since scan produces intermediate results, it's perfect for live tracking metrics like running a total
of movie durations, a cumulative average of ratings, or a progress counter.
For example, if you're analyzing a list of movies, scan can show you how the total duration grows
as each movie is processed.
So to summarize, you can think of scan as a fold with visibility.
It performs the same accumulation logic, but continuously emits partial results.
This makes it ideal for scenarios like dashboards.
progress bar, or running analytics where you want to see every step of the aggregation in real time.
So here is a code example of scan the syntax and everything.
As you can see, this is very similar to fold.
You have the supplier and the buy function.
But the only thing that's different is that for every movie that's been passed from the stream, it's
going to be emitting the running total and you'll be able to see it in the console.
So this helps you visualize how the total evolves as we stream through the data.
Let's go back to the code and start exploring this in action.
Let's go back to the code.
And here I'm back in IntelliJ.
So I'm going to be commenting out the fold operation so that our console looks cleaner.
Let's go to the scan.
Actually we can copy the same code that we wrote for the duration and then put it over here.
So in the place of fold all you got to do is scan.
But instead of collecting the result, what I'm going to do I'm going to be doing a for each.
And then I'm going to give a running total and then print that running total in the console.
Okay.
So we don't need this logger anymore.
There you go.
It's not going to return anything.
Take that off.
So there you go.
We have the scan ready actually.
So let's go ahead and execute this so that you'll be able to see the result on the.
Screen here.
As you can see, for each and every movie, you will be able to see the value being growing little by
little.
So first one is 175.
That's our first movie, and 175 plus 142 is 317 and 317 plus 154 is basically 471.
And this is the beauty of scan where it's going to continuously emit as the gatherer is performing the
summation in our use case.
So it's the same concept for, uh, average ratings and the same concept for titles.
Actually, we can actually explore one of them.
Let's go here copy this and then put it over here.
So in the place of this I'm going to do a scan.
Take this off take this off.
And then we can do for each.
We can actually print the string over here okay.
Let's run this program one more time so you can see that the title is growing one by one.
It started with Godfather and then it added the next movie, it added the next movie, and the list
goes on and on.
So this is the beauty of scan.
You have the ability to see where the accumulation is for each and every element.
That's part of the stream.
This marks the end of this lecture.
Thank you for watching.