Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 30, 2013
Merged

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

merged 2 commits into from
Mar 30, 2013

Conversation

almost
Copy link
Contributor

@almost almost commented Mar 29, 2013

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
Copy link
Contributor Author

almost commented Mar 29, 2013

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 added a commit that referenced this pull request Mar 30, 2013
_.unzip as the inverse of _.zip
@jashkenas jashkenas merged commit c3d6953 into jashkenas:master Mar 30, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants