Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a Clojure namespace to a string?

Tags:

clojure

I'm trying to pretty print a list of namespaces:

(doseq [x (all-ns)] (println x))

This prints each namespace as #<Namespace xxxxx>. I would like to get each namespace as xxxxx (that is without the #<Namespace>. I tried to (name x), (symbol x) but I get ClassCastException clojure.lang.Namespace cannnot be cast to java.lang.Named, etc.

(doseq [x (all-ns)] (println (name x)))
(doseq [x (all-ns)] (println (str x)))
(doseq [x (all-ns)] (println (namespace x)))

How can I get the namespace as a string?

like image 666
RubenLaguna Avatar asked Jan 01 '26 03:01

RubenLaguna


2 Answers

Use ns-name:

(doseq [x (all-ns)] (println (ns-name x)))

Note that ns-name gives you a symbol. So if you want a string just use (str (ns-name ns)).

like image 187
Sven Schoenung Avatar answered Jan 06 '26 06:01

Sven Schoenung


Use the ns-name function:

(doseq [x (all-ns)] (println (ns-name x)))

Namespace function docs can be found here

Best of luck.

like image 21


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!