Created
July 26, 2014 15:11
-
-
Save krrrr38/f2cba5993b75d6beb7e6 to your computer and use it in GitHub Desktop.
This file contains 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
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