yangxu998 / guava-libraries

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

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

Original issue reported on code.google.com by adam.g...@evocatus.com on 18 May 2011 at 4:17

GoogleCodeExporter commented 9 years ago
I think Iterables.concat 
(http://guava-libraries.googlecode.com/svn/tags/release09/javadoc/com/google/com
mon/collect/Iterables.html#concat%28java.lang.Iterable%29) is what your looking 
for.

See also,
http://stackoverflow.com/questions/5949091/flattening-an-iterableiterablet-in-gu
ava

Original comment by raymond....@gmail.com on 18 May 2011 at 4:57

GoogleCodeExporter commented 9 years ago
If not, can you explain the situation where you have an Iterator but not an 
Iterable?

Original comment by raymond....@gmail.com on 18 May 2011 at 4:58

GoogleCodeExporter commented 9 years ago
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.

Original comment by adam.g...@evocatus.com on 18 May 2011 at 6:05

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

Original comment by kevinb@google.com on 19 May 2011 at 2:10

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

Original comment by kevinb@google.com on 19 May 2011 at 2:11

GoogleCodeExporter commented 9 years ago
@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.

Original comment by adam.g...@evocatus.com on 19 May 2011 at 3:04

GoogleCodeExporter commented 9 years ago
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).

Original comment by j...@nwsnet.de on 19 May 2011 at 7:59

GoogleCodeExporter commented 9 years ago
@yo: That is a very good point. I can see `forArray` makes this useful

Original comment by raymond....@gmail.com on 19 May 2011 at 10:35

GoogleCodeExporter commented 9 years ago
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)

Original comment by nev...@gmail.com on 19 May 2011 at 10:51

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

Original comment by j...@nwsnet.de on 19 May 2011 at 5:22

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 6:18

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 7:19

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 13 Jul 2011 at 7:27

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 5 Oct 2011 at 5:02

GoogleCodeExporter commented 9 years ago
Should I do this?

Original comment by wasserman.louis on 18 Oct 2011 at 11:22

GoogleCodeExporter commented 9 years ago
Sure, that'd be great.

Original comment by kevinb@google.com on 20 Oct 2011 at 6:16

GoogleCodeExporter commented 9 years ago

Original comment by yrfselrahc@gmail.com on 5 Dec 2011 at 6:35

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:09

GoogleCodeExporter commented 9 years ago
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?

Original comment by kevinb@google.com on 16 Mar 2012 at 9:54

GoogleCodeExporter commented 9 years ago
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.

Original comment by j...@nwsnet.de on 17 Mar 2012 at 12:30

GoogleCodeExporter commented 9 years ago
Iterators.concatIterables?

Original comment by wasserman.louis on 17 Mar 2012 at 7:39

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09