Skip to content

Instantly share code, notes, and snippets.

@runarorama
Created August 11, 2011 19:51
Show Gist options
  • Save runarorama/1140578 to your computer and use it in GitHub Desktop.
Save runarorama/1140578 to your computer and use it in GitHub Desktop.
ADTs in Scala
trait Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B): B
}
object Just {
def apply[A](a: => A): Maybe[A] = new Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B) = just(a)
}
def unapply[A](a: Maybe[A]): Option[A] = a(Some(_), None)
}
object Nothing {
def apply[A]: Maybe[A] = new Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B) = nothing
}
def unapply[A](a: Maybe[A]): Boolean = a(_ => false, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment