Skip to content

Instantly share code, notes, and snippets.

@BenFradet
Last active May 23, 2017 09:17
Show Gist options
  • Save BenFradet/90e0c956dae4eb8240ce to your computer and use it in GitHub Desktop.
Save BenFradet/90e0c956dae4eb8240ce to your computer and use it in GitHub Desktop.
predicate boolean algebra
import spire.algebra._
import spire.implicits._
object Main {
def main(args: Array[String]): Unit = {
implicit def PredicateBooleanAlgebra[T] = new Bool[T => Boolean] {
def one: T => Boolean = _ => true
def zero: T => Boolean = _ => false
def and(a: T => Boolean, b: T => Boolean): T => Boolean = x => a(x) && b(x)
def or(a: T => Boolean, b: T => Boolean): T => Boolean = x => a(x) || b(x)
def complement(a: T => Boolean) = x => !a(x)
}
def c[T](t: T)(s: Set[T]) = s contains t
val s = Set(1, 2, 3).subsets.map(_.toSet).toSet
println(s.filter(~(c(1) _) & (c(2) _)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment