kucci / guava-libraries

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

Iterables.concat() improvement #446

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I was looking at some code recently, where there was something like an 
Iterable<Foo> where Foo implements Iterable (i.e. informally, a list of lists). 
 The code wanted to flatten these into a single Foo instance.  So I wanted a 
function like

  <T extends Iterable<?>> T concat(Supplier<T> supplier, Iterable<T> ts)

which would construct a new T, using the supplier, consisting of the contents 
of all ts.

If you wanted to get super-Tony-Morris-style geeky, you could further 
generalize this as a fold, something like:

  <T, U> U fold(Function<Pair<T, U>, U> fn, U unit, Iterable<T> ts)

which starts with a "unit" value, and repeatedly applies fn to each element in 
ts, yielding a new accumulated value at each step.  The above concat could then 
be implemented this way.

.... Oh, uh, I guess there's already an enhancement request filed for fold() -- 
issue 218 -- which amusingly was created by the dude whose code I'm looking at. 
 Fun times.

Original issue reported on code.google.com by dan.ro...@gmail.com on 8 Oct 2010 at 11:58

GoogleCodeExporter commented 9 years ago
If the supplier's get() is only going to be called once it seems like you can 
just omit that.

Iterable has no add() method, so presumably you want to operate on Collections.

I wrote this recently:
public static <T,C extends Collection<T>> C addAll(C col, Iterable<? extends 
Collection<? extends T>> values) 
{
    for (Collection<? extends T> val : values) {
         col.addAll(val);
    }
    return col;
}

So I can do:

Multiset<Mineral> totals = 
CollectionUtil.addAll(EnumMultiset.create(Mineral.class), 
listOfMineralMultisets);

...which is basically a folding operation.

Original comment by ray.j.gr...@gmail.com on 11 Oct 2010 at 6:44

GoogleCodeExporter commented 9 years ago
I'm not clear on what is really being asked for here.  Is it different enough 
from that other issue?

Original comment by kevinb@google.com on 8 Apr 2011 at 2:10

GoogleCodeExporter commented 9 years ago
No, not different from issue 218.  I shouldn't have submitted this to begin 
with :P

Original comment by dan.ro...@gmail.com on 8 Apr 2011 at 2:45

GoogleCodeExporter commented 9 years ago

Original comment by cgdec...@gmail.com on 8 Apr 2011 at 3:30

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 3 Nov 2014 at 9:09