Skip to content

Commit

Permalink
Fixes jashkenas#1190 -- remove silly duplicate unzip function. Favor …
Browse files Browse the repository at this point in the history
…_.zip.apply in the docs.
  • Loading branch information
jashkenas committed Jul 8, 2013
1 parent 3fdc395 commit 12ce11f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
15 changes: 2 additions & 13 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@
<li>- <a href="#difference">difference</a></li>
<li>- <a href="#uniq">uniq</a></li>
<li>- <a href="#zip">zip</a></li>
<li>- <a href="#unzip">unzip</a></li>
<li>- <a href="#object">object</a></li>
<li>- <a href="#indexOf">indexOf</a></li>
<li>- <a href="#lastIndexOf">lastIndexOf</a></li>
Expand Down Expand Up @@ -860,19 +859,9 @@ <h2 id="arrays">Array Functions</h2>
<pre>
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=&gt; [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]
</pre>

<p id="unzip">
<b class="header">unzip</b><code>_.unzip(*arrays)</code>
<br />
The opposite of <a href="#zip">zip</a>. Given a number of <b>arrays</b>, returns a
series of new arrays, the first of which contains all of the first elements
in the input arrays, the second of which contains all of the second elements,
and so on. Use with <tt>apply</tt> to pass in an array of arrays.
</p>
<pre>
_.unzip(["moe", 30, true], ["larry", 40, false], ["curly", 50, false]);
=&gt; [['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]]
_.zip.apply(_, arrayOfRowsOfData);
=&gt; arrayOfColumnsOfData
</pre>

<p id="object">
Expand Down
14 changes: 6 additions & 8 deletions test/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,17 @@ $(document).ready(function() {
var names = ['moe', 'larry', 'curly'], ages = [30, 40, 50], leaders = [true];
var stooges = _.zip(names, ages, leaders);
equal(String(stooges), 'moe,30,true,larry,40,,curly,50,', 'zipped together arrays of different lengths');
});

test('unzip', function() {
var stoogesUnzipped = _.unzip(['moe',30, 'stooge 1'],['larry',40, 'stooge 2'],['curly',50, 'stooge 3']);
deepEqual(stoogesUnzipped, [['moe','larry','curly'],[30,40,50], ['stooge 1', 'stooge 2', 'stooge 3']], 'unzipped pairs');
stooges = _.zip(['moe',30, 'stooge 1'],['larry',40, 'stooge 2'],['curly',50, 'stooge 3']);
deepEqual(stooges, [['moe','larry','curly'],[30,40,50], ['stooge 1', 'stooge 2', 'stooge 3']], 'zipped pairs');

// In the case of difference lengths of the tuples undefineds
// should be used as placeholder
stoogesUnzipped = _.unzip(['moe',30],['larry',40],['curly',50, 'extra data']);
deepEqual(stoogesUnzipped, [['moe','larry','curly'],[30,40,50], [undefined, undefined, 'extra data']], 'unzipped pairs');
stooges = _.zip(['moe',30],['larry',40],['curly',50, 'extra data']);
deepEqual(stooges, [['moe','larry','curly'],[30,40,50], [undefined, undefined, 'extra data']], 'zipped pairs with empties');

var emptyUnzipped = _.unzip([]);
deepEqual(emptyUnzipped, [], 'unzipped empty');
var empty = _.zip([]);
deepEqual(empty, [], 'unzipped empty');
});

test('object', function() {
Expand Down
9 changes: 0 additions & 9 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,15 +495,6 @@
// Zip together multiple lists into a single array -- elements that share
// an index go together.
_.zip = function() {
return _.unzip.apply(_, slice.call(arguments));
};

// The complementary operation to `_.zip`. Passed a list of arrays, returns
// a list of arrays, the first of which contains all of the first elements,
// the second the second, and so on. For example, `_.unzip` given
// `['a',1],['b',2],['c',3]` returns the array
// `[['a','b','c'],[1,2,3]]`.
_.unzip = function() {
var length = _.max(_.pluck(arguments, "length").concat(0));
var results = new Array(length);
for (var i = 0; i < length; i++) {
Expand Down

0 comments on commit 12ce11f

Please sign in to comment.