简体   繁体   中英

How can I convert a Clojure namespace to a string?

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?

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)) .

Use the ns-name function:

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

Namespace function docs can be found here

Best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM