MedwynLiang / google-collections

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

Contribution: call to exhaust a given iterator while preserving the last n elements #274

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes I only want the last n elements from an iterator and sometimes I want 
those last n 
elements in the reverse order they came in.  The code to do this is pretty 
simple but not exactly 
one-liners you can chain together to do this, at least not easily.

This contribution is a small patch of code that does the above as a simple one 
line call.  Thought 
this might be useful enough to be considered for inclusion in the Iterators 
library.

Original issue reported on code.google.com by tony.cha...@gmail.com on 24 Oct 2009 at 4:22

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the contribution. Your suggestion is definitely reasonable. The only
question is how often people would want that functionality.

FYI, your methods could return a List instead of an Iterable, while still 
taking an
Iterable as an input parameter. Simply create a subclass of AbstractList that
implements get().

Original comment by jared.l....@gmail.com on 24 Oct 2009 at 5:06

GoogleCodeExporter commented 9 years ago
List is a better return type. Especially because I'd have it expected it not to 
be in
reverse order, but to be able to pass it to Iterables.reverse() if I did want 
it in
reverse order.

Original comment by kevin...@gmail.com on 24 Oct 2009 at 5:30

GoogleCodeExporter commented 9 years ago
Good points, I forgot about AbstractList.  Here's a much simpler implementation 
where the iterable version 
returns a list.  And yeah, the use-case for this is pretty specific.  Thought 
I'd throw it out there anyhow.

Original comment by tony.cha...@gmail.com on 24 Oct 2009 at 7:45

Attachments:

GoogleCodeExporter commented 9 years ago
This issue has been moved to the Guava project (keeping the same id number). 
Simply replace 'google-collections' with 'guava-libraries' in your address 
bar and it should take you there.

Original comment by kevinb@google.com on 5 Jan 2010 at 11:09