Skip to content

Instantly share code, notes, and snippets.

@pr1001
Created November 12, 2011 13:34
Show Gist options
  • Save pr1001/1360526 to your computer and use it in GitHub Desktop.
Save pr1001/1360526 to your computer and use it in GitHub Desktop.
Ruby-style trailing conditionals in Scala
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)
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