Scala 归约操作- - - - -reduce

object 归约操作_reduce {
  def main(args: Array[String]): Unit = {
    val list=List(1,2,3,4,5)
    val result=list.reduce(_+_)
    println("结果1为:"+result)
    println("结果2为:"+list.reduceLeft(_+_))
    println("结果3为:"+list.reduceRight(_+_))
    println("结果4为:"+list.reduce(_-_))     //reduce默认相当于使用reduceLeft
    println("结果5为:"+list.reduceLeft(_-_))
    println("结果6为:"+list.reduceRight(_-_))
  }
}
结果1为:15
结果2为:15
结果3为:15
结果4为:-13
结果5为:-13
结果6为:3

 

posted @ 2017-11-24 11:14  soyosuyang  阅读(2977)  评论(0编辑  收藏  举报