Skip to content

Instantly share code, notes, and snippets.

@thanos
Created October 17, 2016 01:43
Show Gist options
  • Select an option

  • Save thanos/e9ece98d49f92b555f06e21373ed2fa7 to your computer and use it in GitHub Desktop.

Select an option

Save thanos/e9ece98d49f92b555f06e21373ed2fa7 to your computer and use it in GitHub Desktop.
yet Another Coudhdb reduce
function (key, values, rereduce) {
if (rereduce) {
var results = {
'sum': values.reduce(function(a, b) { return a + b.sum }, 0),
'min': values.reduce(function(a, b) { return Math.min(a, b.min) }, Infinity),
'max': values.reduce(function(a, b) { return Math.max(a, b.max) }, -Infinity),
'count': values.reduce(function(a, b) { return a + b.count }, 0),
'sumsqr': values.reduce(function(a, b) { return a + b.sumsqr }, 0)
};
for(var i = 0; i < values.length; i++) {
for(var name in values[i]) {
if (results[name]) results[name] += values[i][name];
else results[name] = values[i][name];
}
}
return results;
} else {
var results = {
'sum': sum(values),
'min': Math.min.apply(null, values),
'max': Math.max.apply(null, values),
'count': values.length,
'sumsqr': (function() {
var sumsqr = 0;
values.forEach(function (value) {
sumsqr += value * value;
});
return sumsqr;
})(),
};
for(var i = 0; i < values.length; i++) {
var name = values[i];
if (name >="1" && name <="5") {
if (results[name]) results[name]++;
else results[name] = 1;
}
}
return results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment