Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Created November 24, 2016 16:07
Show Gist options
  • Select an option

  • Save JoolsF/378baa24d3af86f8b3b48e70bcd3e099 to your computer and use it in GitHub Desktop.

Select an option

Save JoolsF/378baa24d3af86f8b3b48e70bcd3e099 to your computer and use it in GitHub Desktop.
Cat xor example 1
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