Skip to content

Instantly share code, notes, and snippets.

@takkkun
Last active July 3, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takkkun/36eb15a84c3f39ea5b2dee73c1e902a1 to your computer and use it in GitHub Desktop.
Save takkkun/36eb15a84c3f39ea5b2dee73c1e902a1 to your computer and use it in GitHub Desktop.
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