Skip to content

Instantly share code, notes, and snippets.

@utaal
Created December 29, 2014 21:40
Show Gist options
  • Save utaal/de0d9905e92a7badc4df to your computer and use it in GitHub Desktop.
Save utaal/de0d9905e92a7badc4df to your computer and use it in GitHub Desktop.
sweet.js if expression macro
macro iff {
rule {
($x:expr) {
$truebody
...
$truelast:expr
} els {
$falsebody
...
$falselast:expr
}
} => {
(function() {
var res = null;
if ($x) {
$truebody
...
res = $truelast;
} else {
$falsebody
...
res = $falselast
}
return res;
}())
}
}
var fortytwo = 42;
console.log(iff(fortytwo == 42) {
var temp = fortytwo + 2;
"" + temp
} els {
null;
"not!"
});
@utaal
Copy link
Author

utaal commented Dec 29, 2014

Prints

44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment