Skip to content

Instantly share code, notes, and snippets.

@d1egoaz d1egoaz/sequencev2
Created Feb 25, 2015

Embed
What would you like to do?
sequence v2
def sequence[A](a: List[Option[A]]): Option[List[A]] = {
def innerAux[A](b: List[Option[A]], tmp: List[A]): Option[List[A]] = {
b match {
case Nil => Some(tmp)
case None :: _ => None
case Some(h) :: t => innerAux(t, tmp :+ h)
}
}
if(a.isEmpty) None
else innerAux(a, Nil)
}
// test
sequence(List(Some(1), Some(2))) => Some(List(1, 2))
sequence(List(Some(1), None, Some(2))) => None
sequence(List(None, Some(1))) => None
sequence(Nil) => None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.