-
-
Save deanwampler/535150 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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