Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

_.unzip as the inverse of _.zip #1038

Merged
merged 2 commits into from

3 participants

@almost

Based on #1016. I modified @fogus's code to allow tuples of any size, not just pairs.

This function would be very useful I think, I'd love to see it in underscore!

@almost

I'll show some comparisons between the output of my version (shown here as almostUnzip) and @fogus's version (show here as fogusUnzip)

// Pairs (output is the same)
var zippedArray = [['moe',30],['larry',40],['curly',50]];
console.log(fogusUnzip(zippedArray));
// [['moe','larry','curly'],[30,40,50]]
console.log(almostUnzip(zippedArray));
// [['moe','larry','curly'],[30,40,50]]

// Triples
var zippedArray = [['moe',30, 'stooge 1'],['larry',40, 'stooge 2'],['curly',50, 'stooge 3']];
console.log(fogusUnzip(zippedArray));
// [['moe','larry','curly'],[30,40,50]]
console.log(almostUnzip(zippedArray));
// [['moe','larry','curly'],[30,40,50], ['stooge 1', 'stooge 2', 'stooge 3']]

// 1-tuples
var zippedArray = [['moe'],['larry'],['curly']];
console.log(fogusUnzip(zippedArray));
// [['moe','larry','curly'],[undefined,undefined,undefined]]
console.log(almostUnzip(zippedArray));
// [['moe','larry','curly']]

// 0-tuples (can I call them that? :p)
var zippedArray = [[],[],[]];
console.log(fogusUnzip(zippedArray));
// [[undefined,undefined,undefined],[undefined,undefined,undefined]]
console.log(almostUnzip(zippedArray));
// []

// Empty array
var zippedArray = [];
console.log(fogusUnzip(zippedArray));
// [[],[]]
console.log(almostUnzip(zippedArray));
// []

The only one I'm not sure is better is the emtpy array case, but I still don't think special casing pairs is the right way to go.

@jashkenas jashkenas merged commit c3d6953 into jashkenas:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 0 deletions.
  1. +15 −0 test/arrays.js
  2. +19 −0 underscore.js
View
15 test/arrays.js
@@ -129,6 +129,21 @@ $(document).ready(function() {
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
});
+ test('unzip', function() {
+ var stoogesZipped = [['moe',30, 'stooge 1'],['larry',40, 'stooge 2'],['curly',50, 'stooge 3']];
+ var stoogesUnzipped = _.unzip(stoogesZipped);
+ deepEqual(stoogesUnzipped, [['moe','larry','curly'],[30,40,50], ['stooge 1', 'stooge 2', 'stooge 3']], 'unzipped pairs');
+
+ // In the case of difference lengths of the tuples undefineds
+ // should be used as placeholder
+ stoogesZipped = [['moe',30],['larry',40],['curly',50, 'extra data']];
+ stoogesUnzipped = _.unzip(stoogesZipped);
+ deepEqual(stoogesUnzipped, [['moe','larry','curly'],[30,40,50], [undefined, undefined, 'extra data']], 'unzipped pairs');
+
+ var emptyUnzipped = _.unzip([]);
+ deepEqual(emptyUnzipped, [], 'unzipped empty');
+ });
+
test('object', function() {
var result = _.object(['moe', 'larry', 'curly'], [30, 40, 50]);
var shouldBe = {moe: 30, larry: 40, curly: 50};
View
19 underscore.js
@@ -499,6 +499,25 @@
return results;
};
+ // The inverse operation to `_.zip`. If given an array of pairs it
+ // returns an array of the paired elements split into two left and
+ // right element arrays, if given an array of triples it returns a
+ // three element array and so on. For example, `_.unzip` given
+ // `[['a',1],['b',2],['c',3]]` returns the array
+ // [['a','b','c'],[1,2,3]].
+ _.unzip = function(tuples) {
+ var results = [];
+ _.each(tuples, function (tuple, tupleIndex) {
+ _.each(tuple, function (value, itemIndex) {
+ if (results.length <= itemIndex) {
+ results[itemIndex] = [];
+ }
+ results[itemIndex][tupleIndex] = value;
+ });
+ });
+ return results;
+ };
+
// Converts lists into objects. Pass either a single array of `[key, value]`
// pairs, or two parallel arrays of the same length -- one of keys, and one of
// the corresponding values.
Something went wrong with that request. Please try again.