Nulls

Kasper Nielsen kasperni at gmail.com
Sat Sep 22 10:18:54 PDT 2012


>> This gets confusing for reduce, since currently we have:
>>
>>    T reduce(T base, Reducer<T>)    // trying not to upset Doug by
>>    Optional<T> reduce(Reducer<T>)  // saying BinaryOperator<T>
>>
>> If we add a
>>
>>    T reduce(Reducer<T>, T defaultValueIfNone)
>
>
> Why would you add this method if you already have it? :-)
> I didn't necessarily mean that the default argument must always be last.

There is a subtle difference between the two at least with the
currently implementation. Using the base with the reducer or just
returning
defaultValueIfNone in case the stream is empty.

If you have a (possible empty) stream of non-null Integers and you
want to sum them, or return null if the stream is
empty. You have to write your reducer to check if it is given the null base

public Integer reduce(Integer left, Integer right) {
   return left == null ? right : left + right; //should probably check
right also.
}

At least that is how it is implemented now.


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