Skip to content

Instantly share code, notes, and snippets.

@nasadorian
Created November 3, 2018 11:26
Show Gist options
  • Save nasadorian/8f10cc1f89225a932238026cf46e57a7 to your computer and use it in GitHub Desktop.
Save nasadorian/8f10cc1f89225a932238026cf46e57a7 to your computer and use it in GitHub Desktop.
ContainmentSet/BoolSet
import cats.Eq, cats.syntax.eq._
case class BoolSet[A](p: A => Boolean)(implicit eq: Eq[A]) {
def add(a1: A): BoolSet[A] = copy(p = a => p(a) || a === a1)
def contains(a: A): Boolean = p(a)
def remove(a: A): BoolSet[A] = copy(p = a1 => a1 =!= a && p(a1))
def union(other: BoolSet[A]): BoolSet[A] = copy(p = a => p(a) || other.p(a))
def intersect(other: BoolSet[A]): BoolSet[A] = copy(p = a => p(a) && other.p(a))
def difference(other: BoolSet[A]): BoolSet[A] = copy(p = a => p(a) && !other.p(a))
def complement: BoolSet[A] = copy(p = a => !p(a))
}
object BoolSet {
// For testing
implicit val eqInt: Eq[Int] = new Eq[Int] {
override def eqv(x: Int, y: Int): Boolean = x == y
}
def apply[A](as: A*)(implicit eq: Eq[A]): BoolSet[A] =
BoolSet((a: A) => as.foldLeft(false)((p, a1) => p || a1 === a))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment