Skip to content

Instantly share code, notes, and snippets.

@rwebber
Last active March 3, 2019 20:36
Show Gist options
  • Save rwebber/f431b7b58be04d54464191e7184185cc to your computer and use it in GitHub Desktop.
Save rwebber/f431b7b58be04d54464191e7184185cc to your computer and use it in GitHub Desktop.
var dataSet = [];
function debug(printString){
if (DebugMode){
print(printString + "\n");
}
}
function main(){
// DO setup here: ONCE ONLY ************************************************
DebugMode = true; //example usage: debug("string" + var + "\n");
MaxValues = arguments[0]; // no Var makes global
// NOTE: setup arguments[2] as a trigger that is triggered when any value is sent to arguments[1].
// This forces the code to run and allows duplicate values to be entered into the array.
debug("Start DataSet Length = " + dataSet.length + "\n\n");
// main function for LOOP **************************************************
main = function(){
if (arguments[3]){ // input 4 mutated to trigger. Reset the array
dataSet = [];
return; // don't output anything... not required with reset.
}
if (dataSet.length >= MaxValues){
dataSet.splice(0, 1); // remove first value since buffer is at Max
}
dataSet.push(arguments[1]); // add last recieved value to end of array, increasing total.
debug("arguments1 = " + arguments[1] + "\n");
debug("DataSet Length = " + dataSet.length + "\n");
//debug("DataSet = " + dataSet + "\n"); // gets big
// reduce array using a function at add all values contained together.
var Sum = dataSet.reduce(function(accumulator, currentValue, currentIndex, array) {
return accumulator + currentValue;
});
debug("DataSet Sum = " + Sum + "\n");
var Average = Sum / dataSet.length;
debug("DataSet Avg = " + Average + "\n\n");
out = [Average] // set output array
return out; // return out array
};
// RUN ONCE FOR INIT ONLY
var out = []; // declare out
return out; // return from INIT state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment