Skip to content

Instantly share code, notes, and snippets.

@ZJONSSON
Created September 20, 2012 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZJONSSON/3755441 to your computer and use it in GitHub Desktop.
Save ZJONSSON/3755441 to your computer and use it in GitHub Desktop.
d3.f
// Function helpers for various array functions
d3.f = {}
d3.f.get = function (key) {
return function(d) {
return typeof key === "function" ? key(d) : d[key]
}
};
["min","max","extent","sum","mean","median","split"]
.forEach(function(fn){
d3.f[fn] = function(key) {
return function(d) { return d3[fn](d,d3.f.get(key)) }
}
});
["ascending","descending"]
.forEach(function(fn) {
d3.f[fn] = function(key) {
return function(a,b) { return d3[fn](d3.f.get(key)(a),d3.f.get(key)(b)) }
}
})
d3.f.first = function(key) {
return function(d) { return d3.first(d,d3.f.ascending(key))}
}
d3.f.last = function(key) {
return function(d) { return d3.last(d,d3.f.descending(key))}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment