Skip to content

Instantly share code, notes, and snippets.

@commondatageek
Last active December 26, 2015 05:03
Show Gist options
  • Save commondatageek/2f3f9b8ccf1f12779504 to your computer and use it in GitHub Desktop.
Save commondatageek/2f3f9b8ccf1f12779504 to your computer and use it in GitHub Desktop.
;;; collection utilities
(defn multi-filter
"Perform multiple filter operations in one function call."
[fns coll]
(reduce (fn [remaining filter-fn]
(filter filter-fn remaining))
coll
fns))
;;; reflection utilities
(defn flagged-members [obj flags]
"Return the names of all object members having the given flags:
(:public :private :static :final etc)"
(let [flag-fns (map #(fn [x] (% (:flags x))) flags)
refl (clojure.reflect/reflect obj)
flagged (multi-filter flag-fns (:members refl))]
{:methods (sort (distinct (map :name
(filter :return-type
flagged))))
:fields (sort (distinct (map :name
(filter #(not (:return-type %))
flagged))))}))
(defn public-members [obj]
"Return the names of all public members of an object."
[obj]
(flagged-members obj [:public]))
(defn public-methods [obj]
"Return the names of all public methods of an object."
(:methods (flagged-members obj [:public])))
(defn public-fields [obj]
"Return the names of all public fields of an object."
(:fields (flagged-members obj [:public])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment