Scala Reduce操作(简化归约)reduce和fold
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 }
好看请赞,养成习惯:) 本文来自博客园,作者:靠谱杨, 转载请注明原文链接:https://www.cnblogs.com/rainbow-1/p/15827062.html
欢迎来我的51CTO博客主页踩一踩 我的51CTO博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地