Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created July 26, 2014 15:11
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 krrrr38/f2cba5993b75d6beb7e6 to your computer and use it in GitHub Desktop.
Save krrrr38/f2cba5993b75d6beb7e6 to your computer and use it in GitHub Desktop.
scala> case class InvalidData(i: Int)
defined class InvalidData
scala> def firstProcess(i: Int): Either[InvalidData, Int] = try{ Right(10 / i) } catch { case _: Throwable => Left(InvalidData(i)) }
firstProcess: (i: Int)Either[InvalidData,Int]
scala> def secondProcess(d: InvalidData): Either[InvalidData, Int] = try{ Right(10 + d.i) } catch { case _: Throwable => Left(InvalidData(d.i)) }
secondProcess: (d: InvalidData)Either[InvalidData,Int]
scala> for { a <- firstProcess(2).left; b <- secondProcess(a).right } yield b
res3: scala.util.Either[InvalidData,Int] = Right(5)
scala> for { a <- firstProcess(0).left; b <- secondProcess(a).right } yield b
res4: scala.util.Either[InvalidData,Int] = Right(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment