Last active
February 22, 2018 04:53
-
-
Save yoshiyoshifujii/bdd8d668c4639d9279041e61c39d30e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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