List FoldLeft

object FoldLeft {

  def foldLeft[A, B](as: List[A], z: B)(f: (B, A) => B): B =
    as match {
      case Nil    => z
      case h :: t => foldLeft(t, f(z, h))(f)
    }

  def main(args: Array[String]): Unit = {
    println(foldLeft(List("1", "2", "3", "4", "5"), "")("(" + _ + _ + ")"))
    println(foldLeft(List(5), 0)(_ + _))
  }
}
(1(2(3(4(5)))))

 

posted on 2016-04-19 21:54  JonkeyGuan  阅读(167)  评论(0编辑  收藏  举报