Yet another run at reduce

Brian Goetz brian.goetz at oracle.com
Tue Jan 8 10:40:02 PST 2013


>> And then...
>>
>>   into(collection)
>>
>> becomes
>>
>>   reduce(intoList())
>>   reduce(intoSet())
>
> I think, I prefer, reduce(toList()) and reduce(toSet()),
> because their semantics is slightly different than into.

That's reasonable.

>> reduce(intoCollection(ArrayList::new))
>>
>> where intoList() could be as simple as:
>>
>>     public static<T>
>>     Reducer<T,List<T>> intoList() {
>>         return intoCollection(ArrayList<T>::new);
>>     }
>>
>> and intoCollection is as shown below.
>
> intoCollection should specifies that the supplier can be called several
> times, so it should always returns a new collection, i.e. this doesn't
> work:

Right.  Needs to be doc'ed.

> public static<T> Reducer<T,List<T>> intoList() {
>      ArrayList<T> list = new ArrayList<>();
>      return intoCollection(() -> list);
> }
>
> I fear that people will write something like that and only test with a
> serial stream.

A valid fear :(


More information about the lambda-libs-spec-experts mailing list