Skip to content

Instantly share code, notes, and snippets.

@cmstead
Created February 5, 2017 21:11
Show Gist options
  • Save cmstead/cb4328c627fedb3ccad9467e68af4416 to your computer and use it in GitHub Desktop.
Save cmstead/cb4328c627fedb3ccad9467e68af4416 to your computer and use it in GitHub Desktop.
Simple stats API example
var statsApi = (function () {
'use strict';
var square = pow(2);
var squareRoot = pow(0.5);
function getStandardDeviation (values) {
var xBar = getMean(values);
var computedValues = values.map(subtractMeanAndSquare(xBar));
return squareRoot(getMean(computedValues));
}
function getMean (values) {
return sum(values) / values.length;
}
function subtractMeanAndSquare (mean) {
return function (value) {
return square(value - mean);
};
}
function pow(power) {
return function (value) {
return Math.pow(value, power);
}
}
function sum (values) {
return values.reduce(add, 0);
}
function add (a, b) {
return a + b;
}
return {
getMean: getMean,
getStandardDeviation: getStandardDeviation
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment