Option Sequence

package option

object Sequence {

  def sequence[A](a: List[Option[A]]): Option[List[A]] = a match {
    case Nil    => Some(Nil)
    case h :: t => h.flatMap(hh => sequence(t) map (hh :: _))
  }

  def main(args: Array[String]): Unit = {
    val l = List(Some(1), Some(2), Some(3), Some(4))
    val l1 = List(Some(1), Some(2), None, Some(4))
    val l2 = Nil
    println(sequence(l))
    println(sequence(l1))
    println(sequence(l2))
  }

}
Some(List(1, 2, 3, 4))
None
Some(List())

 

posted on 2016-04-24 09:58  JonkeyGuan  阅读(209)  评论(0)    收藏  举报