Skip to content

Instantly share code, notes, and snippets.

@jdolson
Created October 6, 2012 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdolson/3846435 to your computer and use it in GitHub Desktop.
Save jdolson/3846435 to your computer and use it in GitHub Desktop.
A naive covariant set
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