Skip to content

Instantly share code, notes, and snippets.

@yoshiyoshifujii
Last active February 22, 2018 04:53
Show Gist options
  • Save yoshiyoshifujii/bdd8d668c4639d9279041e61c39d30e8 to your computer and use it in GitHub Desktop.
Save yoshiyoshifujii/bdd8d668c4639d9279041e61c39d30e8 to your computer and use it in GitHub Desktop.
def sequence[A, B](s: Seq[Either[A, B]]): Either[A, Seq[B]] =
s.foldRight(Right(Nil): Either[A, List[B]]) {
(e, acc) => for (xs <- acc.right; x <- e.right) yield x :: xs
}
def sequence[A, B](s: Seq[Either[A, B]]): Either[A, Seq[B]] =
s.foldLeft(Right(Seq.empty[B]): Either[A, Seq[B]]) { (acc, e) =>
for (xs <- acc; x <- e) yield xs :+ x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment