-
-
Save ozten/f4a23d85e49a628c4a35 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { | |
rule {(default => $value:expr)} => { | |
else { | |
return $value; | |
} | |
} | |
rule {(rule {$cond:expr} => $value:expr)} => { | |
if($cond) { | |
return $value; | |
} | |
} | |
rule {( | |
$(rule {$cond:expr} => $value:expr) $rest ... | |
)} => { | |
_arms (rule {$cond} => $value) | |
_arms ($rest ...) | |
} | |
} | |
macro cond { | |
rule {{ $arms ... }} => { | |
(function() { | |
_arms($arms ...) | |
})(); | |
} | |
} | |
var x = []; | |
var type = cond { | |
rule {(x === null)} => "null" | |
rule {Array.isArray(x)} => "array" | |
rule {(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
This can also expand nicely to a nested ternary: