scala> val a = Array(20, 12, 6, 15, 2, 9) a: Array[Int] = Array(20, 12, 6, 15, 2, 9)
1、a simple function:
scala> a.reduceLeft(_ + _) res0: Int = 64 scala> a.reduceLeft(_ * _) res1: Int = 388800 scala> a.reduceLeft(_ min _) res2: Int = 2 scala> a.reduceLeft(_ max _) res3: Int = 202、Use a function
scala> val a = Array(20, 12, 6, 15, 2, 9) a: Array[Int] = Array(20, 12, 6, 15, 2, 9) scala> val f = (x:Int, y:Int) => x max y f: (Int, Int) => Int = <function2> scala> a.reduceLeft(f) res0: Int = 20