LJC Lambdas Hackday

Brian Goetz brian.goetz at oracle.com
Tue May 29 08:15:09 PDT 2012


> I think it might well be useful to have sum operate over types
> generally.  So if you have a Stream<T>  and can provide a Mapper<T,T>
> then you can sumBy generally.  Obviously there are a variety of
> mathematical structures that you might want to sum that aren't
> primitives - for example a vector.  I'd probably expect that many
> domain objects are the kind of things that you want to sum over as
> well in some cases.  Presumably any domain object that's a commutative
> monoid is safe for this kind of stuff?
>
> In these cases I think its safe for the signature of sumBy to be:
>
> <T>  T sumBy(Iterable<T>  values, Mapper<T,T>  summer)

You can think of sumBy(IntMapper) as a simplified form of 
mapReduce(Mapper, BinaryOperator) in the cases where the sum operation 
is obvious for the type.  (The primary benefit of a fused mapReduce over 
the components map+reduce is that if the mapper maps to primitives, then 
the boxing can be avoided.)

Zooming out: I think the biggest adoption problem we face is: reduce. 
When confronted with reduce, devs are going to let out a big collective 
"WTF?"  This is another reason why some variant of sum() is important -- 
it is the onramp to understanding reduce.  Asking people to jump from 
imperative addition to reduce in one go is unrealistic.



More information about the lambda-dev mailing list