Created
November 24, 2016 16:07
-
-
Save JoolsF/378baa24d3af86f8b3b48e70bcd3e099 to your computer and use it in GitHub Desktop.
Cat xor example 1
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
| import cats.data.Xor | |
| class Boom(msg:String) extends Error(msg) | |
| def boomXorUnit(blowUp: Boolean): Boom Xor Unit = | |
| if(blowUp) Xor.Left(new Boom("kaboom")) | |
| else new Xor.Right(()) | |
| val left = boomXorUnit(true) | |
| val right = boomXorUnit(false) | |
| left.fold(e => s"I am a left: ${e.getMessage} ", s => s"I am a right: $s") // res0: String = I am a left: kaboom | |
| right.fold(e => s"I am a left: ${e.getMessage} ", s => s"I am a right: $s") // res1: String = I am a right: () | |
| //Right biased so don't have / need right map | |
| left.leftMap(e => s"I am a left: ${e.getMessage}") // res2: cats.data.Xor[String,Unit] = Left(I am a left: kaboom) | |
| right.leftMap(e => s"I am a left: ${e.getMessage}") // res3: cats.data.Xor[String,Unit] = Right(()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment