Skip to content

Instantly share code, notes, and snippets.

@ilcavero
Created November 24, 2011 19:09
Show Gist options
  • Save ilcavero/1392030 to your computer and use it in GitHub Desktop.
Save ilcavero/1392030 to your computer and use it in GitHub Desktop.
a non working scala monad
sealed abstract class Determine[+A] {
def map[B](f: A => B): Determine[B]
def flatMap[B](f: A => Determine[B]): Determine[B]
def filter(p: A => Boolean): Determine[A]
def foreach(b: A => Unit): Unit
}
final case class Known[+A](value: A) extends Determine[A] {
def map[B](f: A => B): Determine[B] = Known(f(value))
def flatMap[B](f: A => Determine[B]): Determine[B] = f(value)
def filter(p: A => Boolean): Determine[A] = if (p(value)) this else Unknown
def foreach(b: A => Unit): Unit = b(value)
}
final case class TBD[A](definer : () => A) extends Determine[A] {
private var value: Known[A] = _
private var decided: Boolean = false
private var listener: List[A => Any] = Nil
def map[B](f: A => B): Determine[B] = {
if (decided)
value.map(f)
else
null
}
def flatMap[B](f: A => Determine[B]): Determine[B] = {
null
}
def filter(p: A => Boolean): Determine[A] = {
null
}
def foreach(b: A => Unit): Unit = {
}
def set(value: A): Unit = { this.value = Known(value) }
}
case object Unknown extends Determine[Nothing] {
def map[B](f: Nothing => B): Determine[B] = this
def flatMap[B](f: Nothing => Determine[B]): Determine[B] = this
def filter(p: Nothing => Boolean): Determine[Nothing] = this
def foreach(b: Nothing => Unit): Unit = {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment