Skip to content

Instantly share code, notes, and snippets.

@grayrest
Created October 11, 2012 19:17
Show Gist options
  • Save grayrest/3874826 to your computer and use it in GitHub Desktop.
Save grayrest/3874826 to your computer and use it in GitHub Desktop.
Simple JS implementation of Prismatic's blog post about Graph
function sum (fn, xs) {
var toRet = 0;
for (var i = 0, ii = xs.length; i < ii; i++) {
toRet += fn(xs[i], i, xs);
}
return toRet;
}
function update(t, o) {
for (var k in o) {
t[k] = o[k];
}
return t;
}
function depGraph(graph) {
var deps = {};
for (var k in statsGraph) {
var fnStr = graph[k].toString();
var fnArgs = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).split(/\s*,\s*/);
deps[k] = fnArgs;
}
return deps;
}
function satisfyDeps(depsList, results) {
var acc = [];
for (var i = 0, ii = depsList.length; i < ii; i++) {
if (typeof results[depsList[i]] == 'undefined') {
return false;
} else {
acc.push(results[depsList[i]]);
}
}
return acc;
}
function eagerCompile(graph) {
return function(startValue) {
var deps = depGraph(graph);
var k = Object.keys(deps);
var res = update({}, startValue);
var processing, args, missingDep = true;
do {
processing = false;
for (var i = 0, ii = k.length; i < ii; i++) {
if (typeof res[k[i]] == 'undefined') {
if (args = satisfyDeps(deps[k[i]], res)) {
res[k[i]] = graph[k[i]].apply(res, args);
processed = true;
missingDep = false
}
}
}
if (missingDep) {
throw 'Missing dependency';
}
} while (processing);
return res;
}
}
var statsGraph = {
n: function(xs) { return xs.length; },
m: function(xs, n) { return sum(function(x){ return x; }, xs) / n; },
m2: function(xs, n) { return sum(function(x){ return x * x; }, xs) / n; },
v: function(m, m2) { return m2 - (m * m); }
};
var stats = eagerCompile(statsGraph);
console.log(stats({xs: [1, 2, 3, 6]}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment