Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created May 16, 2012 15:35
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 rirakkumya/2711351 to your computer and use it in GitHub Desktop.
Save rirakkumya/2711351 to your computer and use it in GitHub Desktop.
[scala] Eitherのサンプル
scala> val l = Left(3)
l: Left[Int,Nothing] = Left(3)
scala> val r = Right(3)
r: Right[Nothing,Int] = Right(3)
scala> def calc = (x:Either[Int,Int]) => x.right.map(_ + 2).left.map(_ * 2).right.map(_ + 3)
culc: Either[Int,Int] => Product with Either[Int,Int] with Serializable
scala> calc(r)
res10: Product with Either[Int,Int] with Serializable = Right(8)
scala> calc(l)
res11: Product with Either[Int,Int] with Serializable = Left(6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment