Helper functions for searching through Clojure namespaces for Vars containing specific bits of metadata.
This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
Go to file
2014-04-09 09:20:48 -04:00
src/clj_metasearch update doc comment with regards to the return value 2014-04-09 09:20:48 -04:00
.gitignore initial commit 2014-03-25 09:03:56 -04:00
LICENSE initial commit 2014-03-25 09:03:56 -04:00
project.clj bump version 2014-03-25 11:22:37 -04:00
README.md update README.md 2014-03-25 11:29:12 -04:00

clj-metasearch

Helper functions for searching through Clojure namespaces for Vars containing specific bits of metadata.

"clj-metasearch version"

Usage

Finding Clojure Vars is fairly simple.

(use 'clj-metasearch.core)

(find-vars #(= (:name %) 'pprint))
=> ({:ns clojure.pprint, :var (var clojure.pprint/pprint)})

find-vars takes a predicate which will be run on the metadata for each Var being checked.

We can get the value of Vars we find by using var-get and then begin using the value right away. For example:

(let [println-fn (-> (find-vars #(= (:name %) 'println))
                     (first)
                     :var
                     (var-get))]
  (println-fn "Hello world!"))
Hello world!
=> nil

By default find-vars will search all Clojure namespaces it can find in the current classpath. We can filter which Clojure namespaces are checked by supplying an additional predicate to find-vars under the :namespace-pred argument. This predicate will be run on each namespace found (the namespace will be passed as a symbol to the predicate).

; no namespace filtering. all namespaces are checked
(find-vars #(= (:name %) 'find-namespaces))
=> ({:ns clj-metasearch.core, :var (var clj-metasearch.core/find-namespaces)}
    {:ns clojure.tools.namespace.find, :var (var clojure.tools.namespace.find/find-namespaces)})

; using namespace filtering
(find-vars
  #(= (:name %) 'find-namespaces)
  :namespace-pred #(not= % 'clj-metasearch.core))
=> ({:ns clojure.tools.namespace.find, :var (var clojure.tools.namespace.find/find-namespaces)})

By default, to help avoid loading a bunch of libraries the first time find-vars is called namespaces are not automatically loaded before being checked. Thusly, you will only be able to find Vars in namespaces that are currently loaded.

find-vars takes an additional optional argument :require-all-namespaces? that allows you to change this behaviour. Passing true will cause each namespace being checked to first be loaded via require.

(find-vars #(= (:name %) 'parse))
=> ()

(find-vars
  #(= (:name %) 'parse)
  :require-all-namespaces? true)
=> ({:ns clojure.xml, :var (var clojure.xml/parse)})

When you use true for :require-all-namespaces?, it would normally be a good idea to supply a namespace predicate via :namespace-pred if at all possible to avoid unnecessarily loading a whole bunch of extra namespaces.

License

Distributed under the the MIT License. See LICENSE for more details.