Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.59 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (ns strangeness.fn-arity
  2.   (:import java.lang.reflect.Method))
  3.  
  4. (defn declared-methods [^Class class]
  5.   (filter #(= class (.getDeclaringClass ^Method %))
  6.           (.getMethods class)))
  7.  
  8. (defn has-arity? [fun arity]
  9.   (let [cls (class fun)
  10.         methods (declared-methods cls)]
  11.     (or (when (instance? clojure.lang.RestFn fun)
  12.           (<= (.getRequiredArity ^clojure.lang.RestFn fun) arity))
  13.         (boolean (some (fn [^Method m]
  14.                          (and (= "invoke" (.getName m))
  15.                               (= (count (.getParameterTypes m)) arity)))
  16.                        methods)))))