Created
August 11, 2011 19:51
-
-
Save runarorama/1140578 to your computer and use it in GitHub Desktop.
ADTs in Scala
This file contains hidden or 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
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