Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.60 KB | None | 0 0
  1.   def Try[A](a: => A): Either[Exception, A] =
  2.     try Right(a)
  3.     catch { case e: Exception => Left(e) }
  4.  
  5.   def sequence[E, A](es: List[Either[E, A]]): Either[E, List[A]] = {
  6. //    Try(es.map(
  7. //      x => x match {
  8. //        case Left(value) => throw new RuntimeException()
  9. //        case Right(value) => value
  10. //      }
  11. //    ))
  12.     es match {
  13.       case Right(value) :: Nil => Right(List(value))
  14.       case Left(value) :: tail => Left(value)
  15.       case Right(value) :: tail => tail.foldLeft(
  16.         x => x match {
  17.           case Right(value) => x ::
  18.         },
  19.         Nil
  20.       )
  21.     }
  22.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement