Last active
September 19, 2019 09:25
-
-
Save ataggart/7f7826507ff99985a7565dd20bba9617 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
(defn arities | |
"Returns a vector of the arities of function f, or of the dispatch | |
function if f is a multifn. | |
Example: | |
=> (arities (fn ([]) ([x]) ([x y & z]))) | |
[0 1 2 :more]" | |
[f] | |
(let [f (if (instance? clojure.lang.MultiFn f) (.dispatchFn f) f) | |
methods (.getDeclaredMethods (class f)) | |
count-params (fn [m] (map #(count (.getParameterTypes %)) | |
(filter #(= m (.getName %)) methods))) | |
invokes (count-params "invoke") | |
do-invokes (map dec (count-params "doInvoke")) | |
arities (vec (sort (distinct (concat invokes do-invokes))))] | |
(if (seq do-invokes) | |
(conj arities :more) | |
arities))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment