zip iterator useful?

500 views
Skip to first unread message

Chris Winters

unread,
Aug 5, 2011, 3:11:21 PM8/5/11
to guava-discuss
In addition to Iterators.concat(), would it be generally useful to
have an Iterators.zip()? It would alternate among its member iterators
until each is exhausted, like the following:

List<String> a = [ "one", "two", "three", "four" ];
List<String> b = [ "fee", "fi" ];
List<String> c = [ "broccoli", "tomato", "potato" ];
List<String> d = [ "purple" ];

Iterator<String> zipped = Iterators.zip( a.iterator(), b.iterator(),
c.iterator(), d.iterator() );
int count = 1;
while ( zipped.hasNext() ) {
System.out.println( count++ + ": " + zipped.next() );
}

Output:

1: one
2: fee
3: broccoli
4: purple
5: two
6: fi
7: tomato
8: three
9: potato
10: four

Thanks for all the work, it's a lifesaver. Eagerly anticipating r10!

Chris

Chris Povirk

unread,
Aug 5, 2011, 3:19:54 PM8/5/11
to Chris Winters, guava-discuss
Hi, Chris,

We have this internally (called "interleave"), and it's gotten a
little traction. Can you file a feature request for us to release it?

http://code.google.com/p/guava-libraries/issues/entry

Thanks.

Chris Winters

unread,
Aug 5, 2011, 3:20:59 PM8/5/11
to Chris Povirk, guava-discuss

You bet. Thank you!

Chris

Louis Wasserman

unread,
Aug 5, 2011, 3:46:27 PM8/5/11
to Chris Winters, Chris Povirk, guava-discuss
FYI, using "zip" to refer to that operation is very confusing to those of us who come from functional backgrounds.

Fred Faber

unread,
Aug 5, 2011, 3:57:55 PM8/5/11
to Louis Wasserman, Chris Winters, Chris Povirk, guava-discuss
But not those who are used to python's zip().

Anyway I'm sure the debate on what to name it has a long and promising future ahead.  Most likely nothing to dwell on in the initial stage of voting up the FR.

Fred

Louis Wasserman

unread,
Aug 5, 2011, 3:59:48 PM8/5/11
to ffa...@faiser.com, Chris Winters, Chris Povirk, guava-discuss
Fred, Python's "zip" agrees with my interpretation of "zip."  The OP seems to be using a definition of "zip" that means "interleave" -- it has nothing to do with e.g. ordered pairs.

Fred Faber

unread,
Aug 5, 2011, 4:02:36 PM8/5/11
to Louis Wasserman, Chris Winters, Chris Povirk, guava-discuss
I see -- so essentially interleave ~= flatten(zip).  I stand corrected.  

Chris Winters

unread,
Aug 6, 2011, 12:17:39 PM8/6/11
to guava-discuss
Apologies for that, 'zip' for some reason was the verb in my brain
around that concept. Glad to be corrected.

Chris

On Aug 5, 3:46 pm, Louis Wasserman <wasserman.lo...@gmail.com> wrote:
> FYI, using "zip" to refer to that operation is *very *confusing to those of
> us who come from functional backgrounds.
>
> Louis Wasserman
> wasserman.lo...@gmail.comhttp://profiles.google.com/wasserman.louis
>

Raymond Rishty

unread,
Aug 6, 2011, 2:19:05 PM8/6/11
to guava-discuss
It's funny, but your idea of "zip" is much more similar to the action of a zipper than either "zip" to mean "compress" or python's "zip" function. Still, everyone would have something in mind other than what is intended. Naming things is one of the two hardest problems in computer science, right?


--

BradW

unread,
Aug 6, 2011, 4:30:28 PM8/6/11
to guava-...@googlegroups.com
+1 to Iterators.interleave().

My first thought when I read Iterators.zip() above was, what would you use an iterator that creates a zip compression for?
The follow-on description and sample implementation code confused me more.  When the alternative naming, interleave,
was mentioned in a reply, I understood.

Sorry, sometimes I'm a little dense.

Reply all
Reply to author
Forward
0 new messages