Skip to content

Instantly share code, notes, and snippets.

@disnet
Created July 17, 2013 22:09
Show Gist options
  • Save disnet/6024991 to your computer and use it in GitHub Desktop.
Save disnet/6024991 to your computer and use it in GitHub Desktop.
cond macro
macro _arms {
case (default => $value:expr) => {
else {
return $value;
}
}
case (case $cond:expr => $value:expr) => {
if($cond) {
return $value;
}
}
case (
$(case $cond:expr => $value:expr) $rest ...
) => {
_arms (case $cond => $value)
_arms ($rest ...)
}
}
macro cond {
case { $arms ... } => {
(function() {
_arms($arms ...)
})();
}
}
var x = [];
var type = cond {
case (x === null) => "null"
case Array.isArray(x) => "array"
case (typeof x === "object") => "object"
default => typeof x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment