Last active
August 29, 2015 14:22
-
-
Save jcouyang/70432287bb4996bfe65c to your computer and use it in GitHub Desktop.
Clojure style arity function
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 caseFunc { | |
case {_ ($args...) {$body... $last:expr}} => | |
{ | |
letstx $len = [makeValue(#{$args...}.length , null)]; | |
return #{ | |
case $len: | |
return (function($args...){$body... return $last}).apply(this, arguments) | |
} | |
} | |
} | |
macro defn{ | |
rule { $name { $(($args (,) ...){$body ...})...} } => { | |
function $name (){ | |
switch(arguments.length){ | |
$(caseFunc ($args...) {$body...}; | |
)... | |
} | |
} | |
} | |
rule { { $(($args (,) ...){$body ...})...} } => { | |
(function (){ | |
switch(arguments.length){ | |
$(caseFunc ($args...) {$body...}; | |
)... | |
} | |
}) | |
} | |
} | |
export defn; |
Author
jcouyang
commented
Jun 3, 2015

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