Syntax decision

Llewellyn Falco isidore at setgame.com
Mon Oct 3 10:07:35 PDT 2011


> On Sat, Oct 1, 2011 at 11:38 PM, Stephen Colebourne
> <scolebourne at joda.org> wrote:
> > Personally, I find the selected syntax OK, but not great, except in
> > the x -> expr form. Thats because I dislike the way that the argument
> > brackets blend into the method parameter brackets when passing to a
> > method as in sort((a,b) -> a - b). I always find its the blending of
> > similar characters that is hardest to read, and why # was useful
> > punctuation. Thats also why I might well prefer a Ruby-like sort(|a,b|
> > a - b), because the characters do not blend in the same way.
> --
>

Maybe it's because I am SO use to lambdas in C#, but I have to disagree with
the "blending" comment.
In fact I had to re-read this paragraph a couple of times because I saw
sort(|a,b|a - b)
as
sort(Ia,bIa - b)

and didn't understand how it was a lambda?
actually even side by side it's a bit hard for me to see the difference now.
perhaps it is my font....
allow me to change the second example to lowercase....

sort(|a,b|a - b)
as
sort(ia,bia - b)

there, that's easier to see.

anyways, of bigger concern to me is there still seems to be a profound
misunderstanding of the purpose of lambdas on this expert thread.


lambdas do not offer ANY new functionality to java, they are just an
alternative syntax to enable the open-closed principle.
and they allow people to chose to remove duplication, where otherwise they
would not.

example: summing up the length of characters in a string

the lambda example of this is
names.sum( (n)->n.length);

easy choice, most of us would do it, but,
this is completely allowed by the current java framework, just without the
syntax

names.sum(
                   new Func<String,Integer>(){
                         public Integer extract(String n){
                            return n.length;
                          }
                    }
                  );

of course that is messy, so it might be easier to write

int sum = 0;
for(String n : names)
{
 sum += n.length;
}


and now we stop favoring the open closed principle in favor of
massive duplication.
(if you doubt that I am right about the human nature of this, do a quick
search in the standard java libraries  for a forloop....)


lambdas allow us to remove duplication, one of the most important of design
principles.
Once we do that, we start to see the profund effects it has, just how much
duplication exists in our code, and how many 'patterns' we have. Entire
frameworks start to pop up when we realize this composibility that
open/closed has provided.

but this isn't because lambda's have added that functionality, they have
not!
They have merely made the choice cheap enough that we actually use it.


Llewellyn Falco
www.approvaltests.com
www.teachingkidsprogramming.org


More information about the lambda-dev mailing list