Iterable::forEach not returning Iterable

Rémi Forax forax at univ-mlv.fr
Sun May 20 05:57:46 PDT 2012


On 05/20/2012 02:21 PM, Tomasz Kowalczewski wrote:
> I understand that forEach is eager and do not suggest it should be
> changed (although I can imagine lazy versions on some specialized
> collections working just fine for the user). This is rather an
> argument towards something that potentially is more user friendly and
> is based on the fluent interface pattern. Iterable::into is eager and
> does return a collection which allows user to chain the calls further,
> why not forEach then?

because there is no type in Java corresponding to the values
of side effects so the return value is void (as usual).

>
> Also, if some collection can be a start to a lazy->lazy->eager chain
> of calls then I do not see why operation such as forEach cannot be a
> start of another such chain (l->l->e->l etc..).

The idea is that if you have some lazy->lazy, it can perhaps be
combined to create only one lazy adapter, by example,
filter(f1).filter(f2) is equivalent to filter(f1 & f2),
eager operations because they force evaluation haven't that property.
So if you want to return an Iterable in forEach, you have to store
all items in a temporary colection or you may have weird behavior.

>
> Tomasz

Rémi



More information about the lambda-dev mailing list