Last active
July 3, 2016 13:48
-
-
Save takkkun/36eb15a84c3f39ea5b2dee73c1e902a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object Main { | |
implicit class Capping[+A](value: A) { | |
def cap[B >: A](limit: B)(implicit ordering: Ordering[B]): B = { | |
if (ordering.lteq(value, limit)) value else limit | |
} | |
} | |
def main(args: Array[String]) { | |
println( 99.cap(100)) // => 99 | |
println(101.cap(100)) // => 100 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment