Lambda syntax, etc

hsyl20 hsyl20 at yahoo.fr
Wed Feb 4 15:25:54 PST 2009


> 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).

Cheers,
Sylvain



More information about the Digitalmars-d mailing list