作为值的函数

scala> val func: Int => Int = x => x*x
func: Int => Int = 

scala> func(4)
res0: Int = 16

scala> val arr = Array(1,2,3,4,5,6)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6)

scala> arr.map(x => func(x))
res1: Array[Int] = Array(1, 4, 9, 16, 25, 36)

scala> def m1(x: Int ) = x * x
m1: (x: Int)Int

scala> arr.map(x => m1(x))
res2: Array[Int] = Array(1, 4, 9, 16, 25, 36)

scala> arr.map(m1(x))
:10: error: not found: value x
              arr.map(m1(x))
                         ^

scala> arr.map(m1)
res4: Array[Int] = Array(1, 4, 9, 16, 25, 36)

scala> arr.map(func)
res5: Array[Int] = Array(1, 4, 9, 16, 25, 36)

你可能感兴趣的:(作为值的函数)