Lambda syntax, etc
Nick Sabalausky
a at a.a
Wed Feb 4 21:11:14 PST 2009
"hsyl20" <hsyl20 at yahoo.fr> wrote in message
news:gmd862$1741$1 at digitalmars.com...
>> In D (with dlibs) it is:
>>
>> auto a = [97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42];
>> auto b = a.filter((int i){return !(i % 2);});
>>
>> In Python:
>>
>> a = [97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42]
>> b = [x for x in a if not(i % 2)]
>
> In Scala :
> val a = List(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42)
> val b = a filter (_ % 2 == 0)
>
> or val b = a filter (e => e % 2 == 0)
> or val b = a filter (e : Int => e % 2 == 0)
> or val b = for (e <- a if (e % 2 == 0)) yield e
>
> 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.
More information about the Digitalmars-d
mailing list