我该如何从clojure中的懒惰序列中创建一个字符串?

时间:2013-02-04 13:18:57

标签: string printing clojure lazy-evaluation

我一直使用str来构造字符串:

user> (str '(1 2 3) " == " '(1 2 3))
"(1 2 3) == (1 2 3)"

大约每天一次我被屁股咬伤:

user> (str '(1 2 3) " == " (map identity '(1 2 3)))
"(1 2 3) == clojure.lang.LazySeq@7861"

我想我可以说:

user> (with-out-str (print '(1 2 3) " == " (map identity '(1 2 3))))
"(1 2 3)  ==  (1 2 3)"
相反,但它看起来很难看。还有更好的方法吗?

2 个答案:

答案 0 :(得分:4)

您可以使用print-str

(print-str '(1 2 3) " == " (map identity '(1 2 3)))
;; => "(1 2 3) == (1 2 3)"

答案 1 :(得分:1)

您可以使用seq

将LazySeq对象转换为Cons one对象
user=> (str '(1 2 3) " == " (map identity '(1 2 3)))
"(1 2 3) == clojure.lang.LazySeq@7861"

user=> (str '(1 2 3) " == " (seq (map identity '(1 2 3))))
"(1 2 3) == (1 2 3)"