val twice1: Double => Double = x => x * 2
val twice2: Double => Double = _ * 2
val twice3 = (x: Double) => x * 2
val twice4 = (_: Double) * 2
Scala将上面表达式转化为Function1,实现Function1的apply方法
val twice = new Function1[Double, Double] {
def apply(x: Double): Double = x * x
}
val lt1: (Double, Double) => Boolean = (x, y) => x < y
val lt2: (Double, Double) => Boolean = _ < _
val lt3 = (x: Double, y: Double) => x < y
val lt4 = (_: Double) < (_: Double)
Scala将表达式转化为Function2,实现Function2的apply方法