Created
November 12, 2011 13:34
-
-
Save pr1001/1360526 to your computer and use it in GitHub Desktop.
Ruby-style trailing conditionals in Scala
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
class Conditional[T](left: => T) { | |
def iff(right: => Boolean): Option[T] = if (right) Some(left) else None | |
def unless(right: => Boolean): Option[T] = if (!right) Some(left) else None | |
} | |
implicit def any2Unless[T](left: => T) = new Conditional(left) |
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
scala> val k = 1001 | |
k: Int = 1001 | |
scala> k iff k % 2 == 0 | |
res9: Option[Int] = None | |
scala> k unless k % 2 == 0 | |
res10: Option[Int] = Some(1001) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment