MeiChun-Lo / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

Consider adding Zip operator #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Consider adding Zip operator for sequences, somewhat akin to the built-in 
zip function in Python. See:
http://www.python.org/doc/2.5.2/lib/built-in-funcs.html#l2h-81

Zip has the following logical behavior given two sequences:

zip((1, 2, 3), (4, 5, 6)) -> ((1, 4), (2, 5), (3, 6))

It returns tuples, where each tuple contains the N-th element from each of 
the argument sequences. The returned sequence is truncated in length to 
the length of the shortest argument sequence.

The attached patch contains a proposed implementation along with unit 
tests. I think Zip belongs in the grouping category of operators (since it 
groups elements by their position).

It looks like Zip will make it into .NET Framework 4.0 (see 
http://codebetter.com/blogs/matthew.podwysocki/archive/2008/11/16/functiona
l-net-4-0-tuples-and-zip.aspx) so all the better to make it available to 
3.5 applications via this project.

Original issue reported on code.google.com by azizatif on 13 Feb 2009 at 12:01

Attachments:

GoogleCodeExporter commented 9 years ago
Fabulous - this was certainly something I was going to do, so it's wonderful to 
have 
it done for me :) Will check out the code tonight and almost certainly just put 
it 
in.

Original comment by jonathan.skeet on 13 Feb 2009 at 6:27

GoogleCodeExporter commented 9 years ago
Looks okay at a quick look, but I have two suggestions: 

- I think we want four possible strategies for the case where the two sequences 
aren't the same length: IgnoreLonger (current implementation); FillWithDefault 
(e.g. 
{1,2} and {"a", "b", "c"} would zip to {{1,"a"}, {2,"b"}, {0,"c"}} and Throw 
(throw 
InvalidOperationException).

- The current implementation doesn't dispose of the iterators after it's done.

I'll look at adding both of these aspects tonight.

Original comment by jonathan.skeet on 15 Feb 2009 at 4:56

GoogleCodeExporter commented 9 years ago
Okay, making a few changes now - should commit tonight.

Original comment by jonathan.skeet on 15 Feb 2009 at 5:54

GoogleCodeExporter commented 9 years ago

Original comment by azizatif on 15 Feb 2009 at 6:51

GoogleCodeExporter commented 9 years ago
Completed in r11.

Original comment by jonathan.skeet on 15 Feb 2009 at 8:25