Skip to content

Instantly share code, notes, and snippets.

@astrangeguy
Created October 30, 2010 02:46
Show Gist options
  • Save astrangeguy/654851 to your computer and use it in GitHub Desktop.
Save astrangeguy/654851 to your computer and use it in GitHub Desktop.
(ns strangeness.fn-arity
(:import java.lang.reflect.Method))
(defn declared-methods [^Class class]
(filter #(= class (.getDeclaringClass ^Method %))
(.getMethods class)))
(defn has-arity? [fun arity]
(let [cls (class fun)
methods (declared-methods cls)]
(or (when (instance? clojure.lang.RestFn fun)
(<= (.getRequiredArity ^clojure.lang.RestFn fun) arity))
(boolean (some (fn [^Method m]
(and (= "invoke" (.getName m))
(= (count (.getParameterTypes m)) arity)))
methods)))))
@astrangeguy
Copy link
Author

user> (map #(has-arity? (fn
([])
([a b])
([a b c d & e]))
%)
[0 1 2 3 4 5 6 7 8 9 10])

(true false true false true true true true true true true)

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