In this lecture, we were going to learn how to accumulate values with the Reduce operator, the Reduce
operator is the counterpart to the array reduce function.
If you're familiar with the reduced function on arrays, understanding the Reduce operator will be a
breeze.
In some cases, we may want to accumulate the values emitted from an observable.
The Reduce operator accomplishes this action.
Let's say our observable emitted the values one through five.
The observer may want the sum of these values.
Instead of letting the observer handle this computation, we can add the reduced function to our pipeline
every time a value is emitted.
The Reduce operator will take in the value until the next value is emitted.
Once the observable completes the reduce operator, it will push a value onto the observer throughout
this entire process.
The observer will receive a single value.
We can modify how values are accumulated by passing in a function.
In this example, we are adding the values together.
Let's dive into the arguments through our code.
For this example, let's create a stream of numbers.
This stream will be reduced to a single value with the Reduce operator.
First, we will update the import statement for the creation operators.
We will import the of operator.
As we learned, the Earth operator will allow us to create a synchronous stream of data next completely
replace the from event operator with the operator.
The other operator will limit numbers one through five.
As it stands, the numbers will be emitted one by one, we can intercept each value with the reduce
operator to accumulate them.
Let's give it a try by importing the Reduce operator from the Oryx SJS slash operators package.
In the pipeline, replace the current operators with the Reduce operator.
The Reduce operator has two arguments, which are a function and seed value.
Let's pass in an empty function and a seed value of zero.
The seed value is optional, but typically you will want to add it.
This value refers to the starting value.
The goal is to get the sum of the values emitted from the observable.
Our starting value should start at zero.
New numbers are added to the seed value.
Next, we can start accumulating the values through the arrow function.
This function will be supplied with two arguments called accumulator and value.
The accumulator argument stores the value previously returned by this function, if this function is
being called for the first time, the accumulator argument will store the seed value.
As for the value argument, it references the value emitted by the observable.
We must return a value since we're trying to get the sum.
We all have these two arguments together.
In the end, we will receive the sum in the console, the behavior of the reduced operator is entirely
different from other operators.
Typically, each time a value is pushed, the value would get lost in the console.
However, in this scenario, the reduced operator does not push each value emitted from the observable.
It's waiting until the observable has been completed.
After the observable completes, the reduce operator will push the value it has accumulated over time.
This operator does introduce some problems.
What if the observable it's reading from doesn't complete?
Let's look at how we can tackle this scenario in the following lecture.