Question about Iterable.reduce signature
François Sarradin
fsarradin at gmail.com
Sun May 20 23:25:31 PDT 2012
Hi all,
I'm looking for an answer to a question about the method reduce. But, I
still haven't found it in archives of the mailing list. So here is my
question:
The current signature of Iterable<T>.reduce is:
T reduce(T base, BinaryOperator<T> reducer)
I would like to know why the signature doesn't look like this one?
<U> U reduce (U base, BiOp<T, U> reducer)
where BiOp<T, U> is supposed to represent a function of two arguments of
respective types T and U, that returns a value of type U.
I really think that this other signature can lead to more readable
code. Below is an example with, say, a set of products that you want to buy
in whatever store you want. And you want to know the total.
With the current version of reduce:
double total = products.map(p -> p.getPrice()).reduce(0.0, (price,
subTotal) -> price + subTotal);
With the other version of reduce in this mail:
double total = products.reduce(0.0, (p, subTotal) -> p.getPrice() +
subTotal);
I know that there is a method named mapReduce. But I'm still uncomfortable
with it:
double total = products.mapReduce(p -> p.getPrice(), 0.0, (price,
subTotal) -> price + subTotal);
Regards,
Francois
More information about the lambda-dev
mailing list