Created
November 3, 2018 11:26
-
-
Save nasadorian/8f10cc1f89225a932238026cf46e57a7 to your computer and use it in GitHub Desktop.
ContainmentSet/BoolSet
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
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