List SumByFoldLeft

object Sum {

  def sum(as: List[Int]): Int = FoldLeft.foldLeft(as, 0)(_ + _)

  def main(args: Array[String]): Unit = {
    println(sum(Nil))
    println(sum(List(2)))
    println(sum(List(1, 2, 3, 4, 5)))
  }
}
0
2
15

 

posted on 2016-04-19 22:11  JonkeyGuan  阅读(119)  评论(0编辑  收藏  举报