Skip to content

Commit

Permalink
Add d3.zip.
Browse files Browse the repository at this point in the history
Analagous to Python's built-in zip():
<http://docs.python.org/library/functions.html#zip>

Fixes #145.
  • Loading branch information
jasondavies committed May 27, 2011
1 parent 98ef2d1 commit 2403293
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ d3.core.js: \
src/core/descending.js \
src/core/min.js \
src/core/max.js \
src/core/zip.js \
src/core/bisect.js \
src/core/nest.js \
src/core/keys.js \
Expand Down Expand Up @@ -167,6 +168,7 @@ tests: \
tests/test-keys.test \
tests/test-nest.test \
tests/test-permute.test \
tests/test-zip.test \
tests/test-remove.test \
tests/test-rgb.test \
tests/test-round.test \
Expand Down
10 changes: 10 additions & 0 deletions d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ d3.max = function(array, f) {
}
return a;
};
d3.zip = function() {
var n = arguments.length,
length = d3.max(arguments, function(d) { return d.length; }),
results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = new Array(n);
for (var j = 0; j < length; j++) results[i][j] = arguments[j][i];
}
return results;
};
// Locate the insertion point for x in a to maintain sorted order. The
// arguments lo and hi may be used to specify a subset of the array which should
// be considered; by default the entire array is used. If x is already present
Expand Down
4 changes: 2 additions & 2 deletions d3.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/core/zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
d3.zip = function() {
var n = arguments.length,
length = d3.max(arguments, function(d) { return d.length; }),
results = new Array(length);
for (var i = 0; i < length; i++) {
results[i] = new Array(n);
for (var j = 0; j < length; j++) results[i][j] = arguments[j][i];
}
return results;
};
10 changes: 10 additions & 0 deletions tests/test-zip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require("./../lib/env-js/envjs/node");
require("./../d3");

console.log("zip [1, 2] [3, 4]:");
console.log(" " + d3.zip([1, 2], [3, 4]));
console.log("");

console.log("zip [1, 2] [3, 4] [5, 6, 7]:");
console.log(" " + d3.zip([1, 2], [3, 4], [5, 6, 7]));
console.log("");
6 changes: 6 additions & 0 deletions tests/test-zip.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
zip [1, 2] [3, 4]:
1,3,2,4

zip [1, 2] [3, 4] [5, 6, 7]:
1,3,5,2,4,6,,,7

0 comments on commit 2403293

Please sign in to comment.