Nice to @Share?

Alex Buckley Alex.Buckley at Sun.COM
Tue Feb 23 05:51:37 PST 2010


I would love to have a library of examples that show lambdas using tail 
recursion rather than imperative side effects. You get entry #0 and a 
credit in "The Java Programming Language (5th Edition)".

Does anyone else have examples?

Alex

Rémi Forax wrote:
> By the way, I am very happy with final (or implicit final).
> When I code and find that I need a non-final local variable,
> I first try to rewrite the code to avoid to use non-final
> before trying to use a mutable object.
> 
> <T> void foreach(List<T> list, #void(T) fun) {
>    for(String element: list)
>      fun.invoke(element);
> }
> ...
> @shared int length = 0;
> foreach(alist, #(String element){
>     length += element.length;
> });
> 
> can be rewritten as:
> 
> <T> int reduce(List<T> list, #int(T, int) fun, int acc) {
>    for(String element: list)
>      acc+= fun.invoke(element);
>     }
>     return acc;
> }
> ...
> int length = reduce(alist, #(String element, int acc) {
>     return element.length + acc;
> });
> 
> In my opinion, @Shared is not that important if you provide APIs
> to easily play with collections.
> 
> [...]
> 
>> --Reinier Zwitserloot
>>    
> 
> Rémi
> 


More information about the lambda-dev mailing list