Last Updated: May 04, 2019
·
1.767K
· chris_betz

Apropos improved

I like apropos. I really do. Just describe your problem, and apropos will come up with a solution. Sometimes.

Example? (apropos "reflection") returns warn-on-reflection.

What I do not like about the standard clojure apropos ist, that it returns no information about the namespaces. So, in my current project, when I run (apropos "filter"), I get (filter filter-keys-by-val filter-vals filter-keys). But where is this filter-keys-by-val? Which namespace should I refer to, when calling it?

So I made apropos+. It returns (#’clojure.core/filter #’utilize.map/filter-keys-by-val #’utilize.map/filter-vals #’utilize.map/filter-keys).

(ns dev.apropos)

(defn apropos+
  "Given a regular expression or stringable thing, return a seq of
all definitions in all currently-loaded namespaces that match the
str-or-pattern."
  [str-or-pattern]
  (let [matches? (if (instance? java.util.regex.Pattern str-or-pattern)
    #(re-find str-or-pattern (str (key %)))
    #(.contains (str (key %)) (str str-or-pattern)))]
    (for [ns (all-ns)
          public (ns-publics ns)
          :when (matches? public)]
      (second public)
      )))

;; (in-ns 'user)
;; (use 'dev.apropos)
;; (apropos+ "*warn")

(see https://gist.github.com/2873882) for a Gist.