Skip to content

Instantly share code, notes, and snippets.

@halcat0x15a
Forked from gakuzzzz/gist:2383028
Created April 14, 2012 09:29
Show Gist options
  • Save halcat0x15a/2383148 to your computer and use it in GitHub Desktop.
Save halcat0x15a/2383148 to your computer and use it in GitHub Desktop.
計算中と計算終了を表すコンテナ
trait Container[+R, +C] {
def map[A](f: C => A): Container[R, A]
def flatMap[RR >: R, A](f: C => Container[RR, A]): Container[RR, A]
def toResult[RR >: R](implicit ev: C <:< RR): Result[RR]
}
case class Calculating[+R, +C](element: C) extends Container[R, C] {
def map[A](f: C => A): Container[R, A] = Calculating(f(element))
def flatMap[RR >: R, A](f: C => Container[RR, A]): Container[RR, A] = f(element)
def toResult[RR >: R](implicit ev: C <:< RR): Result[RR] = Result(element)
}
case class Result[+R](element: R) extends Container[R, Nothing] {
def map[A](f: Nothing => A): Container[R, A] = this
def flatMap[RR >: R, A](f: Nothing => Container[RR, A]): Container[RR, A] = this
def get: R = element
def toResult[RR >: R](implicit ev: Nothing <:< RR): Result[RR] = this
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment