Stateful Running Transformation

Overview


A stateful running transformation is a transformation which maintains a separate state that is used to populate the desired calculation.

As an example, a transformation that calculates a moving average might carry along a running total and other information in order to do the necessary calculation for each record.
Moving Average Examples
The moving average example creates funciton which maintains the necessary state (running total and running values) necessary to calculate each required moving average.

The following creates a moving average function which maintains the necessary state.

function runningMovingAverage(size){
  let total = 0;
  let items = [];
  return function(item){
    total += item;
    items.push(item);
    if(items.length>size){
      let first = items.shift();
      total -=first;
    }
    if(items.length === size) return total/size;
    return null;
  }
}
				
The following code uses the above defined function in order to utilize the array map function to transform the data.


let data = [{price:100},{price:101},{price:100},{price:102},{price:102},{price:103},{price:105},{price:102}];
let ma = runningMovingAverage(3);
let results = data.map(p=>{
  return {
	price:p.price,
	movingAverage:ma(p.price)
  }
});
				
Try it!

Contents