Effectively final

Stephen Colebourne scolebourne at joda.org
Mon Aug 15 17:51:39 PDT 2011


On 15 August 2011 22:14, Brian Goetz <brian.goetz at oracle.com> wrote:
>> So I think it's better to disappoint some people and educate everyone to
>> use reduce:
>> int sum = parallelArray.reduce( 0, #{ element, sum ->  sum + element });
>
> Or, more likely:
>
>   int sum = collection.reduce(0, Reducers.INT_SUM);

I will note that while I understand the trade off here, I think that
reduce is going to be harder for Java developers to adapt to than a
"filter" or "apply/execute", with "transform/mapper" being in the
middle. Neither reduce form above is especially pretty or obvious.

In other words, I believe that this is conceptually easier to
understand than reduce, especially for Java developers:

int total = 0;
list.apply(#{item -> total += item});

Now, the above can in theory be rewritten into a reduce at compile
time. So, I have mused on various syntax sugar options (for
discussion, not checked for parseability/practicality etc. etc.):

int total = 0 : list.apply(#{item -> total += item});

int total = 0 + list.apply(#{item -> total += item});

int total = 0 -> list.apply(#{item -> total += item});

int total = 0 {
  list.apply(#{item -> total += item});
};

int total = list.combine(total = 0 : #{item -> total += item});

int #total = 0;
list.apply(#{item -> total += item});

ie. a way to introduce a local variable that can be managed safely.

I'd also note that there is a wider decision as to whether to use
"map" and "reduce" or something more "Java friendly" like "transform"
and "combine/summarize".
Stephen


More information about the lambda-dev mailing list