Adam Schmideg
Adam Schmideg

Reputation: 10850

Get available clojure namespaces

Is there an idiomatic way to get available namespaces that can be used?

(all-ns) returns only already used namespaces. (Package/getPackages) returns all Java packages available for import, but only those Clojure namespaces that are already used.

Then I stumbled upon this post, but it uses some classpath magic.

So I want to get something like ('clojure.core 'clojure.set ... 'clojure.contrib.accumulators 'clojure.contrib.condition ...) if I have the clojure.jar and contrib.jar on my classpath, but I haven't used anything yet.

Upvotes: 6

Views: 1393

Answers (2)

kotarak
kotarak

Reputation: 17299

You will need to do "classpath magic". Since there is no kind of registry, you have to walk the classpath and look in every clojure source file to determine what namespaces are available. (In case the files are not AOT compiled. Otherwise you'll need a different heuristic.)

I think the function used in the linked post is the best way to go: clojure.contrib.find-namespaces/find-namespaces-on-classpath.


Deprecated since Clojure 1.3.0; use now clojure.tools.namespace.find/find-namespaces and clojure.java.classpath/classpath from http://github.com/clojure/java.classpath

Upvotes: 9

mikera
mikera

Reputation: 106351

I have found bultitude to be a great tool for doing this.

Example:

user=> (require '[bultitude.core :as b])
nil

user=> (take 10 (b/namespaces-on-classpath))
(bultitude.core-test bultitude.core clojure.data clojure.string clojure.test clojure.xml clojure.inspector clojure.repl clojure.set clojure.test.junit)

user=> (b/namespaces-on-classpath :prefix "bultitude")
(bultitude.core-test bultitude.core)

Upvotes: 2

Related Questions