Skip to content

Instantly share code, notes, and snippets.

@jcouyang
Last active August 29, 2015 14:22
Show Gist options
  • Save jcouyang/70432287bb4996bfe65c to your computer and use it in GitHub Desktop.
Save jcouyang/70432287bb4996bfe65c to your computer and use it in GitHub Desktop.
Clojure style arity function
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;
@jcouyang
Copy link
Author

jcouyang commented Jun 3, 2015

> arifunc('foo','bar')
2 arity foo bar
undefined
> arifunc('foo')
1 arity foo
undefined
> arifunc('foo','bar', 'hehe')
undefined

@jcouyang
Copy link
Author

jcouyang commented Jun 3, 2015

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