Created
May 18, 2010 02:40
-
-
Save kmizu/404541 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
object MyMain extends Application { | |
abstract sealed class Either[+A,+B] { | |
def map[C, D >: A](f:B => C) : Either[D,C] | |
def flatMap[C, D >: A](f:B => Either[D,C]) : Either[D,C] | |
} | |
case class Right[A,B](b:B) extends Either[A,B] { | |
def map[C, D >: A](f:B => C) = Right(f(b)) | |
def flatMap[C, D >: A](f:B => Either[D,C]) = f(b) | |
} | |
case class Left[A,B](a:A) extends Either[A,B] { | |
def map[C, D >: A](f:B =>C) = Left(a) | |
def flatMap[C, D >: A](f:B => Either[D,C]) = Left(a) | |
} | |
def div(a:Int, b:Int) = if(b==0) Left ("div by 0") else Right(a/b) | |
val e = Right[String,Int](1) | |
val g = for(x <- e; y <- div(x,0)) yield y | |
println(g) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment