Primitive streams (original) (raw)

Raab, Donald Donald.Raab at gs.com
Fri Dec 28 15:01:11 PST 2012


We thought it would be worthwhile for GSC.

Here's an example usage from our Kata in the Order class, getValue() method:

return this.lineItems.sumOfDouble(LineItem::getValue);  

vs.

return this.lineItems.stream().map(LineItem::getValue).reduce(0.0, (x, y) -> x + y);

The second one does require more awareness and understanding. I understand the second one, but I would use the first one if I needed a sum and it was available on the API.

I assume sum would look like this on streams if/when you add the Double version of stream:

return this.lineItems.stream().map(LineItem::getValue).sum();

and the short-hand would look as follows:

return this.lineItems.stream().sumBy(LineItem::getValue);

The short-hand version might be easier to discover in the IDE. Eventually, Java developers will wade into the deeper end of the pool with map and reduce.

> While many Java programmers are unfamiliar with reduce, there are many > FP-aware folks (ruby, groovy, etc) who will want to transfer their > favorite expressions to Java. We shouldn't go out of or way to make > this transfer difficult.

No, we're not going to make this difficult. Those already familiar with reduce should be pretty happy. The question is, what should we do to accomodate the other 95% of java developers? Giving them reduce only seems like throwing them in the deep end of the pool.



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