Skip to content

Instantly share code, notes, and snippets.

@everson
Created February 17, 2015 17:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everson/42b6124ec3a75205a804 to your computer and use it in GitHub Desktop.
Save everson/42b6124ec3a75205a804 to your computer and use it in GitHub Desktop.
Monadic Either
type Id = Int
def insertA():Either[Exception, Id] = {println("running A"); Right(1)}
def insertB(a: Id):Either[Exception, Id] = {println("running B with " + a); Left(new Exception("Oops"))}
def insertC(b: Id):Either[Exception, Id] = {println("running C with " + b); Right(3)}
for {
a <- insertA().right
b <- insertB(a).right
c <- insertC(b).right
} yield {
c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment