简体   繁体   English

如何将Clojure命名空间转换为字符串?

[英]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> . 这会将每个名称空间打印为#<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. 我想将每个命名空间设置为xxxxx (没有#<Namespace> 。我尝试(name x)(symbol x)但我得到ClassCastException clojure.lang.Namespace cannnot be cast to java.lang.Named ,等等

(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 : 使用ns-name

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

Note that ns-name gives you a symbol. 请注意, ns-name为您提供符号。 So if you want a string just use (str (ns-name ns)) . 所以如果你想要一个字符串,只需使用(str (ns-name ns))

Use the ns-name function: 使用ns-name函数:

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

Namespace function docs can be found here 命名空间函数文档可以在这里找到

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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