1 package chapter07 2 3 object Test15_HighLevelFunction_Reduce { 4 def main(args: Array[String]): Unit = { 5 val list = List(1,2,3,4) 6 7 // 1. reduce 8 println(list.reduce( _ + _ )) 9 println(list.reduceLeft(_ + _)) 10 println(list.reduceRight(_ + _)) 11 12 println("===========================") 13 14 val list2 = List(3,4,5,8,10) 15 println(list2.reduce(_ - _)) // -24 16 println(list2.reduceLeft(_ - _)) //-24 17 println(list2.reduceRight(_ - _)) // 3 - (4 - (5 - (8 - 10))), 6 18 19 println("===========================") 20 // 2. fold 21 println(list.fold(10)(_ + _)) // 10 + 1 + 2 + 3 + 4 22 println(list.foldLeft(10)(_ - _)) // 10 - 1 - 2 - 3 - 4 23 println(list2.foldRight(11)(_ - _)) // 3 - (4 - (5 - (8 - (10 - 11)))), -5 24 } 25 }