On 12/15/10 4:42 PM, retard wrote:
Wed, 15 Dec 2010 16:33:43 -0600, Andrei Alexandrescu wrote:

On 12/15/10 4:18 PM, retard wrote:
Wed, 15 Dec 2010 22:23:35 +0100, Jacob Carlborg wrote:
Array(1, 2, 3, 4, 5).sortWith(_>   _)

The first instance of _ (from left to right) is replaced with the first
element of the parameter tuple, the second with second element, etc.

This is actually very useful since many lambdas only use 1-2
parameters. It has its limitations. For example referring to the same
parameter requires a named parameter or some other hack. Combined with
Haskell style partial application this allows stuff like:

Array(1, 2, 3, 4, 5).foreach { println }

Array(1, 2, 3, 4, 5).filter(2<)

For short lambdas I prefer Phobos' convention of using "a" and "b", e.g.
"2<  a" or "a<  b". Since it's a string, "_<  _" would have been usable
with Phobos too but I wouldn't like such a change.

I haven't have time to test this (still using D1 + Tango), but these
magical 'a' and 'b' make me wonder whether there are any namespace
issues. Can you refer to symbols defined in the current module or in the
Phobos module the collection function is declared? Do the 'a' and 'b'
shadow some other instances of 'a' and 'b'?

There are no hygiene issues, but lookup is limited to the modules included in std.functional. That's goes well with the charter of short lambdas - I mean, they are short :o).

Andrei

Reply via email to