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.
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.
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.