When dealing with sets of data, it's often desirable to reduce that set of data to a single composite value. To demonstrate this, I'm going to use the Array's reduce method. Array.reduce simply takes an argument of a function that is my reduced value, along with an item from the Array. You return a new reduced value. Finally, you can initialize the reduced value with the last argument.
What this is going to do is it's going to pass zero and the first value from my Array, which is also zero, add them together, and then pass whatever it's added together in as the next argument, along with the next value in the Array, and add those together.
We can see what the result is. I'm going to output the results in console. We can see the result is 10, 10 being 01 + 2 + 3 + 4. Let's try the same thing with Observable.
Observable actually also has the reduce method. If I take my Array and turn it into an Observable, we can do the same thing, but since Observables can be asynchronous, we can get our result this way. We need to get our result by subscribing.
Clear and run it again. We get exactly the same answer.
As I stated before, Observables can be asynchronous. Let's try this another way. Let's take an interval. We'll only take the first five values.
An interval is going to output zero, one, two, but it's going to do it once every tenth of a second. I'm going to run this. There's a slight delay, and then it outputs the number 10.
I want to show you something. If I make this one second and I run this -- 1,001, 1,002, 1,003, 1,004. There's my response. You'll see it actually waited for the entire Observable to complete before the result of the reduce was emitted. This is important to remember.
Reduce on an Observable waits for completion of the Observable before it gives you the result. That means if I was to get rid of this take and I have an Observable that will run forever, like this interval, I will never get a response back from reduce.
What if I wanted to see the result of my reduction at each tick from my source? To do that, we use scan. Scan works exactly the same way as reduce, with the exception that its initializer comes first.
Now, when I run this, you'll see each time it ticks in, I get the next value, the next reduced value. One nice difference with scan though is, since it doesn't wait for completion, if I were to run this again, it's actually going to give me a result every time.