Too terse and too alien?

Alessio Stalla alessiostalla at gmail.com
Wed Jun 2 01:51:17 PDT 2010


On Wed, Jun 2, 2010 at 10:06 AM, Florian Weimer <fweimer at bfk.de> wrote:
> * Alessio Stalla:
>
>> Really? I forgot to mention no local variables, since Java doesn't have let.
>> I can come up with reasonable counterexamples:
>>
>> sorting people by surname and name (middle name omitted for brevity :D):
>>
>> Collections.sort(people, #(Person p, Person q) (
>>   p.getSurname().compareTo(q.getSurname()) != 0 ?
>>   p.getSurname().compareTo(q.getSurname()) :
>>   p.getName().compareTo(q.getName()) ));
>>
>> vs
>>
>> Collections.sort(people, #(Person p, Person q) {
>>   int surnameComparison = p.getSurname().compareTo(q.getSurname());
>>   return surnameComparison != 0 ? surnameComparison :
>> p.getName().compareTo(q.getName())
>> });
>
> This is not a realistic example.  It will look more like this in
> JDK 7:
>
>  Collections.sort(people, mkComparator(Person#getSurname(), Person#getName()));
>
> People will write combinator libraries soon to make this programming
> style popular.
>
> (I'm not sure about the exact syntax.)
>
>> processing all event handlers in a list:
>>
>> Collections.map(list, #(EventListener e) {
>>   try { e.handle(event); } catch(Exception e) { log.error("Exception
>> while processing listener", e); }
>> });
>
>  Collections.map(list, suppressException1(e#handle(EventListener),
>  #(Exception e) (log.error("Exception while processing listener", e))));
>
> and so on.

Ok, thanks, both you and Neal Gafter made some valid points for
single-expression closures, and I now think that they will probably be
far more popular than I previously thought. I'm still not convinced
about distinguishing single vs multi-expression/statement at the
syntax level, and about often using combinators instead of writing
code the usual way in the body of the closure, but this falls into the
realm of personal preference.

Regards,
Alessio


More information about the lambda-dev mailing list