Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created May 18, 2010 02:40
Show Gist options
  • Save kmizu/404541 to your computer and use it in GitHub Desktop.
Save kmizu/404541 to your computer and use it in GitHub Desktop.
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