Created
October 5, 2010 12:36
-
-
Save kawaguchi/611478 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
(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) |
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
(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