Last active
January 23, 2017 15:19
-
-
Save fomkin/1af45467518fe9ccd45a1e359ef34732 to your computer and use it in GitHub Desktop.
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
def evenlyDivisibleBy(divider: Int): Int ^^ Int = Extractor { | |
case n if n % divider == 0 => n | |
} | |
val evenlyDivisibleByFive = evenlyDivisibleBy(5) | |
val evenlyDivisibleByThree = evenlyDivisibleBy(3) | |
val evenlyDivisibleByFifteen = evenlyDivisibleBy(15) | |
def fizzbuzz(x: Int) = x match { | |
case evenlyDivisibleByFifteen(_) => "fizzbuzz" | |
case evenlyDivisibleByFive(_) => "buzz" | |
case evenlyDivisibleByThree(_) => "fizz" | |
case _ => x | |
} | |
(1 until 100).map(fizzbuzz).foreach(println) |
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 Extractor[A, R](val f: PartialFunction[A, R]) { | |
def unapply(x: A): Option[R] = f.lift(x) | |
def |(other: Extractor[A, R]): Extractor[A, R] = new Extractor(f.orElse(other.f)) | |
} | |
type ^^[A, R] = Extractor[A, R] | |
object Extractor { | |
def apply[A, R](f: PartialFunction[A, R]) = | |
new Extractor[A, R](f) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment