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

Maybe add Iterables.concat(Iterator<? extends Iterable<? extends T>>) overload (returning Iterator) #627

Open
gissuebot opened this issue Oct 31, 2014 · 22 comments

Comments

@gissuebot
Copy link

gissuebot commented Oct 31, 2014

Original issue created by adam.g...@evocatus.com on 2011-05-18 at 04:17 PM


I routinely need to do something like

public static <T> Iterator<T> flatten(final Iterator<? extends Iterable <T>> it);

In other words flatten an iterator of collection like objects.

It would be nice to have this in com.google.common.collect.Iterators

@gissuebot
Copy link
Author

@gissuebot
Copy link
Author

Original comment posted by raymond.rishty on 2011-05-18 at 04:58 PM


If not, can you explain the situation where you have an Iterator but not an Iterable?

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by adam.g...@evocatus.com on 2011-05-18 at 06:05 PM


Sure I could use concat by abusing the Iterable contract and making a throw away Iterable.

I'm using Iterators.partition to generate an on demand grouping of items that I then need to turn back into the iterator I want. My mind partition and flatten go hand and hand.

The iterator I'm passing in is on demand and very big (million +).

Its not like its hard to code this... I just would not have to do repeat this for all my projects.

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-05-19 at 02:10 AM


I think this request makes sense, especially since it's our own Iterators.partition() method that returns this beast in the first place.


Status: Accepted

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-05-19 at 02:11 AM


For now, the workaround is to transform the outer Iterator with a Function<Iterable, Iterator> that calls .iterator(), then concat that.

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by adam.g...@evocatus.com on 2011-05-19 at 03:04 AM


@kevinb I agree with your solution (I think its what I even used). I only like flatten because its some what brings back warm fuzzy feelings I have when I used Lisp/Scheme/Ocaml... ah the glory days of academic FP.

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2011-05-19 at 07:59 AM


Isn't Guava's concat() a flatten with a depth of 1?

raymond: I just recently found myself using the Iterators class (maybe for the first time) because one can pass varargs to Iterators.forArray() and then use methods mostly found in Iterables (which most Guava users probably know better). Although I haven't compared the difference in overhead between using forArray() instead of, say, creating a list (from varargs/an array), but I just assume the former is more lightweight (correct me if I'm wrong).

@gissuebot
Copy link
Author

Original comment posted by raymond.rishty on 2011-05-19 at 10:35 AM


@yo: That is a very good point. I can see forArray makes this useful

@gissuebot
Copy link
Author

Original comment posted by neveue on 2011-05-19 at 10:51 AM


If you want to work with Iterables / Collections instead, you can use java.util.Arrays.asList(T... a), which create a list backed by the given array. There is no instantiation overhead (since the List is a "view" of the source array). It lets you work with more powerful data-structures :)

Other interesting methods:

  • com.google.common.collect.Lists.asList(@Nullable E first, E[] rest)
  • com.google.common.collect.Lists.asList(@Nullable E first, @Nullable E second, E[] rest)

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2011-05-19 at 05:22 PM


nev: Thanks for the pointer. I tend to forget about Arrays.asList() because it has "array" in its name ;)

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-07-13 at 06:18 PM


(No comment entered for this change.)


Status: Triaged

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-07-13 at 07:19 PM


(No comment entered for this change.)


Status: Accepted

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-07-13 at 07:27 PM


(No comment entered for this change.)


Labels: Type-Enhancement

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-10-05 at 05:02 AM


(No comment entered for this change.)


Labels: Milestone-Release11

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2011-10-18 at 11:22 PM


Should I do this?

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2011-10-20 at 06:16 AM


Sure, that'd be great.

@gissuebot
Copy link
Author

Original comment posted by yrfselrahc on 2011-12-05 at 06:35 PM


(No comment entered for this change.)


Labels: -Milestone-Release11

@gissuebot
Copy link
Author

Original comment posted by fry@google.com on 2011-12-10 at 04:09 PM


(No comment entered for this change.)


Labels: Package-Collect

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-03-16 at 09:54 PM


Oh crud. This method can't be added to Iterators. It's not a valid overload after erasure. It could be added to Iterables, but that's not where I'd have expected to see it at all. It would be the only method of Iterables to return Iterator. Is it worth doing that?

@gissuebot
Copy link
Author

gissuebot commented Oct 31, 2014

Original comment posted by j...@nwsnet.de on 2012-03-17 at 12:30 PM


If type erasure prohibits overloading, adding it under a different name would work. That also comes with a price tag, though.

That said, I'm currently not into this issue, so it's neither a +1 nor -1 from me.

@gissuebot
Copy link
Author

Original comment posted by wasserman.louis on 2012-03-17 at 07:39 PM


Iterators.concatIterables?

@gissuebot
Copy link
Author

Original comment posted by kevinb@google.com on 2012-05-30 at 07:43 PM


(No comment entered for this change.)


Labels: -Type-Enhancement, Type-Addition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants