Skip to content

Instantly share code, notes, and snippets.

@d1egoaz d1egoaz/sequence
Last active Aug 29, 2015

Embed
What would you like to do?
sequence
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 h :: t =>
h match {
case Some(v) => innerAux(t, v :: tmp)
case None => None
}
}
}
innerAux(a, Nil)
}
// test
sequence(List(Some(1), Some(2))) => Some(List(2, 1))
sequence(List(Some(1), None, Some(2))) => 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.