Skip to content

Commit

Permalink
Cleaner comments, all reduction functions return add, rem, ini, acc, …
Browse files Browse the repository at this point in the history
…for brevity
  • Loading branch information
wssbck committed Nov 14, 2014
1 parent 90f2fa0 commit ea3d2aa
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 76 deletions.
141 changes: 104 additions & 37 deletions crossfilter-helpers.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,183 @@
var exports = (function(e){
'use strict';

// reduce by the number of elements of a group
/**
reduce by the number of elements of a group
initial value: 0
*/
e.count = function(){
return {
add : function(p, v){
p.count++;
return p;
},
remove : function(p, v){
rem : function(p, v){
p.count--;
return p;
},
init : function(){
return { count : 0 };
ini : function(){
return {
count : 0
};
},
access : function(p){
acc : function(p){
return p.count;
}
}
};

// reduce by the sum of elements of a group
/**
reduce by the sum of elements of a group
initial value: 0
*/
e.sum = function(fn){
return {
add : function(p, v){
p.sum += fn(v);
return p;
},
remove : function(p, v){
rem : function(p, v){
p.sum -= fn(v);
return p;
},
init : function(){
return { sum : 0 };
ini : function(){
return {
sum : 0
};
},
access : function(p){
acc : function(p){
return p.sum;
}
}
};

// reduce by the product of elements of a group, initially set to 1
/**
reduce by the product of elements of a group
initial value: 1
*/
e.product = function(fn){
return {
add : function(p, v){
p.product *= fn(v);
return p;
},
remove : function(p, v){
p.product /= fn(v);
rem : function(p, v){
p.product = fn(v) != 0 ? p.product / fn(v) : p.product;
return p;
},
init : function(){
return { product : 1 };
ini : function(){
return {
product : 1
};
},
access : function(p){
acc : function(p){
return p.product;
}
}
};

// reduce by maximum value, initially set to -Infinity
/**
reduce by maximum value among elements of a group
initial value: -Infinity
*/
e.max = function(fn){
return {
add : function(p, v){
p.max = fn(v) > p.max ? fn(v) : p.max;
return p;
},
remove : function(p, v){
rem : function(p, v){
p.max = fn(v) > p.max ? fn(v) : p.max;
return p;
},
init : function(){
return { max : -Infinity };
ini : function(){
return {
max : -Infinity
};
},
access : function(p){
acc : function(p){
return p.max;
}
}
};

// reduce by minimum value, initially set to +Infinity
/**
reduce by minimum value among elements of a group
initial value: Infinity
*/
e.min = function(fn){
return {
add : function(p, v){
p.min = fn(v) < p.min ? fn(v) : p.min;
return p;
},
remove : function(p, v){
rem : function(p, v){
p.min = fn(v) < p.min ? fn(v) : p.min;
return p;
},
init : function(){
return { min : Infinity };
ini : function(){
return {
min : Infinity
};
},
access : function(p){
acc : function(p){
return p.min;
}
}
};

// reduce by the (weighted) mean of elements of a group
// if weight is not set, constant 1 is assumed
// if weight equals 0, sum is not changed (current record is ignored)
// if count equals 0, mean is zeroed
// reduce by spread, meaning the difference between the mininum and the maximum of the group
// e.spread = function(fn){
// return {
// add : function(p, v){
// p.min = fn(v) < p.min ? fn(v) : p.min;
// return p;
// },
// rem : function(p, v){
// p.min = fn(v) < p.min ? fn(v) : p.min;
// return p;
// },
// ini : function(){
// return {
// spread : 0,
// min : Infinity,
// max : -Infinity
// };
// },
// acc : function(p){
// return p.spread;
// }
// }
// };

/**
reduce by the (weighted) mean of elements of a group
if weight is not set, constant 1 is assumed
initial value: 0
*/
e.mean = function(fn_f, fn_w){
var w = typeof fn_w === 'function' ? fn_w : e.helper_constant(1);
var w = typeof fn_w === 'undefined' ? e.helper_constant(1) : fn_w;
return {
add : function(p, v){
p.count += w(v);
p.sum += fn_f(v) * w(v);
p.count += w(v);
p.mean = p.count ? p.sum / p.count : 0;
return p;
},
remove : function(p, v){
p.count -= w(v);
rem : function(p, v){
p.sum -= fn_f(v) * w(v);
p.count -= w(v);
p.mean = p.count ? p.sum / p.count : 0;
return p;
},
init : function(){
return { count : 0, sum : 0, mean : 0 };
ini : function(){
return {
count : 0,
sum : 0,
mean : 0
};
},
access : function(p){
acc : function(p){
return p.mean;
}
}
Expand All @@ -137,6 +191,19 @@ module.exports = exports;
var exports = (function(e){
'use strict';

// build a general data accessor for reduce() parameter functions
// if f is a function, it will return this function
// otherwise it will build a function that, given a datum d,
// will return d[f]
e.helper_accessor = function(f) {
if (typeof f != 'function') {
return function(d) {
return d[f];
}
}
return f;
}

// build a function returning a selected variable
e.helper_constant = function(c) {
return function() {
Expand Down
2 changes: 1 addition & 1 deletion crossfilter-helpers.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ea3d2aa

Please sign in to comment.