Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def Try[A](a: => A): Either[Exception, A] =
- try Right(a)
- catch { case e: Exception => Left(e) }
- def sequence[E, A](es: List[Either[E, A]]): Either[E, List[A]] = {
- // Try(es.map(
- // x => x match {
- // case Left(value) => throw new RuntimeException()
- // case Right(value) => value
- // }
- // ))
- es match {
- case Right(value) :: Nil => Right(List(value))
- case Left(value) :: tail => Left(value)
- case Right(value) :: tail => tail.foldLeft(
- x => x match {
- case Right(value) => x ::
- },
- Nil
- )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement