|
> The first two uses of () are anonymous functions No they're not. They are just containers for arguments to a function. In Scala whenever you call a function which takes as it's only parameter another function, you can even replace them entirely with curly brackets: scala> (1 to 5).map(_ * 5)
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(5, 10, 15, 20, 25)
is equivalent to: scala> 1 to 5 map { _ * 5 }
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(5, 10, 15, 20, 25)
The _ symbol is equivalent to the % in your Clojure example. So both examples above are equivalent to: scala> (1 to 5).map((x:Int) => x * 5)
res2: scala.collection.immutable.IndexedSeq[Int] = Vector(5, 10, 15, 20, 25)
You don't need _any_ characters or visual indicators to declare an anonymous function. Example: scala> def apply(op: Int => Int, y: Int) = op(y)
apply: (op: (Int) => Int, y: Int)Int
Can then be called as: scala> apply(_ * 10, 10)
res4: Int = 100
Or: scala> apply((x:Int) => x * 10, 10)
res5: Int = 100
Or even: scala> apply((_ * 10), 10)
res6: Int = 100
Perhaps your original point stands that in Clojure you have _better_ visual clues for picking out anonymous functions at a glance, but I disagree that you even need them in the Scala code you posted. |
Yes, this is what I was trying to get at. Thanks for clarifying the examples, though. So, using _ in any expression makes that expression an anonymous function? Is _ by itself the identity function?