Created
June 25, 2014 16:06
-
-
Save eamelink/52d93742ab6d914f03bf to your computer and use it in GitHub Desktop.
Composable predicates
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
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