Skip to content

Instantly share code, notes, and snippets.

@kawaguchi
Created October 5, 2010 12:36
Show Gist options
  • Save kawaguchi/611478 to your computer and use it in GitHub Desktop.
Save kawaguchi/611478 to your computer and use it in GitHub Desktop.
引数の数を調べる
(arity (symbol-function 'identity)) ;=> (1 . 1)
(arity (symbol-function '+)) ;=> (0 . many)
(arity (symbol-function 'substring)) ;=> (2 . 3)
(defun foo (a b c &optional d e f))
(arity (symbol-function 'foo)) ;=> (3 . 6)
(defun bar (a b c &rest rest))
(arity (symbol-function 'bar)) ;=> (3 . many)
(defun arity (func)
"Return minimum and maximum number of args"
(if (subrp func)
(subr-arity func)
(let ((min 0) (max 0) optional)
(catch 'many
(mapcar
(lambda (arg)
(cond ((eq arg '&optional)
(setq optional t))
((eq arg '&rest)
(setq max 'many)
(throw 'many t))
(t
(unless optional
(setq min (1+ min)))
(setq max (1+ max)))))
(help-function-arglist func)))
(cons min max))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment