List FoldLeftByFoldRight

object FoldLeftByFoldRight {

  def foldLeft[A, B](l: List[A], z: B)(f: (B, A) => B): B = FoldRight.foldRight(Reverse.reverse(l), z)((a: A, b: B) => f(b, a))

  def main(args: Array[String]): Unit = {

    println(foldLeft(List("1", "2", "3", "4", "5"), "")("(" + _ + _ + ")"))
    println(foldLeft(List(5), 0)(_ + _))

  }

}
(((((1)2)3)4)5)
5

 

posted on 2016-04-20 22:01  JonkeyGuan  阅读(227)  评论(0编辑  收藏  举报