Created
April 12, 2012 17:25
-
-
Save adinapoli/2369342 to your computer and use it in GitHub Desktop.
when operator in scala
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
/** | |
* A syntactic sugar corresponding to the Lisp's when. Execute the body | |
* when the pred evaluates to true. | |
* @param condition A predicate or value that evaluate to a Boolean | |
* @param block A valid block of scala code | |
*/ | |
def when (condition : => Boolean)( block : => Unit) | |
{ | |
if (condition){ block } | |
} | |
//A left associative version from the redditor elbowich | |
implicit def `when for function0`[A](f: => A) = new { | |
def when(cond: Boolean) = if(cond) Some(f) else None | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yep! Thanks for the suggestion!
Bye!
Alfredo