Created
July 17, 2013 22:09
-
-
Save disnet/6024991 to your computer and use it in GitHub Desktop.
cond macro
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
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