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.

Andrei

Reply via email to