Lambda syntax, etc

Yigal Chripun yigal100 at gmail.com
Thu Feb 5 11:18:54 PST 2009


hsyl20 wrote:
> Nick Sabalausky Wrote:
>>> The first notation "_ % 2 == 0" has no boilerplate and Scala is statically
>>> typed (unlike Python).
>>>
>> I like that very much, especially since you can use either the implicit _ or
>> a manually named var. Although I would prefer something like "a", "b", etc,
>> (or maybe "_1", "_2",. etc) instead of "_", because "_" doesn't seem to lend
>> itself well to multi-arg lambdas, for instance, with reduce(). I don't like
>> *needing* to name a var when it's something trivial like in the above
>> examples.
>
> You can use several "_", for instance:
> scala>  val a = List(10,5,2,48,75,84,96,85,3,21,52)
> a: List[Int] = List(10, 5, 2, 48, 75, 84, 96, 85, 3, 21, 52)
>
> scala>  val b = a reduceLeft (_ + _)
> b: Int = 481
>
> The only problem is if you want to change arg order. In this case you have to use named parameters.
> scala>  val b = a reduceLeft (_ - _)
> b: Int = -461
>
> scala>  val b = a reduceLeft ((a,b) =>  b - a)
> b: Int = -5
>
> Cheers
> Sylvain

this seems counter-intuitive to me. Nemerle uses this syntax for 
currying which seems to me a much better meaning to this syntax.
for example ( using D like syntax):

int func (string a, char b, int c) { ... }
auto a = func( "hello", _, 8);

the above is syntax sugar for:

auto a = int(char b) { return func("hello", b, 8); };




More information about the Digitalmars-d mailing list