Skip to content

Instantly share code, notes, and snippets.

@apipkin
Forked from lehrblogger/gist:1054238
Created June 29, 2011 16:30
Show Gist options
  • Save apipkin/1054246 to your computer and use it in GitHub Desktop.
Save apipkin/1054246 to your computer and use it in GitHub Desktop.
function (key, values, rereduce) {
// value -> [count, min, max, mean]
var i = 0
, l = values.length
, min = values[i][2] // set min to the first max
, max = 0
, weighted_sum_of_means = 0
, count = 0
, cur_count // just for clarity, feel free to remove later
, cur_min
, cur_max
, cur_mean
;
for (; i < l; i++) {
cur_count = values[i][0];
cur_min = values[i][1];
cur_max = values[i][2];
cur_mean = values[i][3];
count = count + cur_count;
min = Math.min(min, cur_min);
max = Math.max(max, cur_max);
weighted_sum_of_means = weighted_sum_of_means + (cur_count * cur_mean);
}
return [count, min, max, weighted_sum_of_means / count];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment