Skip to content

Instantly share code, notes, and snippets.

@eamelink
Created June 25, 2014 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamelink/52d93742ab6d914f03bf to your computer and use it in GitHub Desktop.
Save eamelink/52d93742ab6d914f03bf to your computer and use it in GitHub Desktop.
Composable predicates
class SimpleFile
class ExtendedFile extends SimpleFile
implicit class Predicate[-A](val fn: A => Boolean) {
def &&[B <: A](other: Predicate[B]): Predicate[B] = Predicate { (x: B) =>
fn(x) && other.fn(x)
}
}
val alwaysTrueSimple = (_: SimpleFile) => true
val alwaysTrueAdvanced = (_: ExtendedFile) => true
alwaysTrueSimple && alwaysTrueAdvanced
alwaysTrueAdvanced && alwaysTrueSimple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment