Skip to content

Instantly share code, notes, and snippets.

@ataggart
Last active September 19, 2019 09:25
Show Gist options
  • Save ataggart/7f7826507ff99985a7565dd20bba9617 to your computer and use it in GitHub Desktop.
Save ataggart/7f7826507ff99985a7565dd20bba9617 to your computer and use it in GitHub Desktop.
(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