Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Forked from sadache/Error.scala
Created August 18, 2010 15:33
Show Gist options
  • Save deanwampler/535150 to your computer and use it in GitHub Desktop.
Save deanwampler/535150 to your computer and use it in GitHub Desktop.
case class Error[E,A](e:Either[E,A]) {
def flatMap[B](f:A => Error[E,B]):Error[E,B] ={
Error(e.right.flatMap(a=> f(a).e))
}
def map[B](f:A=>B):Error[E,B]={
Error(e.right.map(f))
}
}
object ErrorUtils{
implicit def eitherToError[E,A](e:Either[E,A]):Error[E,A] = Error(e)
implicit def eitherToError[E,A](e:Error[E,A]):Either[E,A] = e.e
}
scala> import ErrorUtils._
scala> for (i <- Some(1).toRight("Some possible Error");
j <- Right(2):Error[String,Int]) yield i+j
res13: Error[String,Int] = Error(Right(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment