Skip to content

Instantly share code, notes, and snippets.

@kaja47
Created October 16, 2011 15:26
Show Gist options
  • Save kaja47/1291030 to your computer and use it in GitHub Desktop.
Save kaja47/1291030 to your computer and use it in GitHub Desktop.
Boolean function composition
trait F[T] extends Function1[T, Boolean] {
def apply(t: T): Boolean
def &&(g: T => Boolean) = (a: T) => apply(a) && g(a)
def ||(g: T => Boolean) = (a: T) => apply(a) || g(a)
def ^ (g: T => Boolean) = (a: T) => apply(a) ^ g(a)
def unary_! = (a: T) => !apply(a)
}
implicit def toF[T](a: T => Boolean) = new F[T] { def apply(x: T) = a(x) }
// ***
val f: Int => Boolean
val g: Int => Boolean
val h: Int => Boolean
val c: Int => Boolean = !f && g || h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment