Created
March 3, 2011 15:04
-
-
Save dexterous/852887 to your computer and use it in GitHub Desktop.
No more if's; SmallTalk style conditionals in Groovy
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
class BooleanCategory { | |
public static Boolean then(Boolean self, Closure c) { | |
if(self) c() | |
return self | |
} | |
public static Boolean otherwise(Boolean self, Closure c) { | |
if(!self) c() | |
return self | |
} | |
} |
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
use(BooleanCategory) { | |
('foo'.length() > 2).then { println 'yipee' }.otherwise { println 'bummer' } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run this at http://groovyconsole.appspot.com/script/431002 or https://ideone.com/CCzIl