Handling a Complete Stream with Reduce

InstructorJohn Lindquist

Share this video with your friends

Send Tweet

When a stream has completed, you often need to evaluate everything that has happened while the stream was running. This lesson covers how to use reduce to collect values and total up a “score” of this simple game.

ganqqwerty
~ 6 years ago

what's the difference between reduce and scan? It seems that they do the same thing, but reduce operator emits a value only when its source observable completes. Scan will spit out values all the time.

J. Matthew
~ 5 years ago

Yes, I think that's right. scan lets you accumulate a value and make use of it throughout the life of a stream, like how we were incrementing the timer and logging it each tick in prior lessons. It doesn't notify you (or make itself available) once the stream completes. reduce lets you summarize the entire life of the stream once the stream completes, but you can't read or make use of the summary before that point.