Here, we define the size of the moving average window (let's say we are interested in the most recent five events).
Let's generate some data with the range operator.
With the index operator, we may attach an index to each of the elements.
With the scan operator, we collect the latest five elements into a container (4.1), where the element's index is used to calculate the position in the container (4.2). On every step, we return the same container with the updated content.
Here, we skip some elements at the beginning of the stream to gather enough data for the moving average.
To calculate the value of the moving average, we divide the sum of the container content on its size.
Of course, we have to subscribe for data to receive values.