Lambda syntax, etc
hsyl20
hsyl20 at yahoo.fr
Wed Feb 4 22:59:52 PST 2009
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
More information about the Digitalmars-d
mailing list