Created
October 6, 2012 22:57
-
-
Save jdolson/3846435 to your computer and use it in GitHub Desktop.
A naive covariant set
This file contains 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 Set[+A] extends (Any => Boolean) { self => | |
def contains(x: Any): Boolean | |
def apply(x: Any): Boolean = contains(x) | |
def + [B >: A](x: B): Set[B] = new Set[B] { | |
def contains(y: Any) = (y == x) || self.contains(y) | |
} | |
def union[B >: A](that: Set[B]): Set[B] = new Set[B] { | |
def contains(x: Any) = self.contains(x) || that.contains(x) | |
} | |
def intersect[B >: A](that: Set[B]): Set[B] = new Set[B] { | |
def contains(x: Any) = self.contains(x) && that.contains(x) | |
} | |
} | |
object EmptySet extends Set[Nothing] { | |
def contains(x: Any) = false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment