List FlatMap

object FlatMap {

  import FoldRight._
  import AppendByFoldLeft._

  def flatMap[A, B](as: List[A])(f: A => List[B]): List[B] = foldRight(as, Nil: List[B])((a: A, b: List[B]) => append(f(a), b))

  def main(args: Array[String]): Unit = {
    println(flatMap(List(1, 2, 3))(i => List(i, i)))
  }

}
List(1, 1, 2, 2, 3, 3)

 

posted on 2016-04-23 08:25  JonkeyGuan  阅读(526)  评论(0编辑  收藏  举报