Clojure打印延迟序列 [英] Clojure printing lazy sequence

查看:164
本文介绍了Clojure打印延迟序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试着打印出我的二进制树,但Clojure很难打印出正确的序列。 '(1 2 3)例如。



在每次迭代中,

 (defn spaces [n] 
(apply str(repeat n )))

很好,这似乎工作。


$ b b

所以,假设我有一个节点的列表 '(:a:b:c)

 (println(map#(str(spaces before)% (spaces(dec before)))nodes))



我有一个项目列表。使用地图我得到一个字符串对象的列表。非常好,我可以打印它们!



但这给了我:

 (clojure.lang.LazySeq@d0b37c31 clojure.lang.LazySeq@105879a9 clojure.lang.LazySeq@8de18242)

所以,我googled如何打印惰性序列,我来到使用 print-str 命令。根据文档,这打印到一个字符串,然后得到返回。

 (println空格前)%(空格(十进制前)))节点)))

 (clojure.lang.LazySeq@d0b37c31 clojure.lang.LazySeq@105879a9 clojure.lang.LazySeq@8de18242)

没有变化.. Hrm。非常感谢任何帮助。

解决方案

  (str(map inc(range 10)))
clojure.lang.LazySeq@c5d38b66
user> (pr-str(map inc(range 10)))
(1 2 3 4 5 6 7 8 9 10)

toString 方法由 str ,这避免了通过不透明地显示Object标识来实现值的延迟序列。 pr-str 函数调用对象的 print-dup 多重方法,读者可以理解的事情(因此对于 LazySeq 的字面值,使得等于 LazySeq )。



对于结构的漂亮格式化,请检查 clojure.pprint 命名空间,其中包含 clojure.core ,它有 pprint print-table 自定义漂亮打印的行为。

  user> (require'[clojure.pprint:as pprint:refer [pprint print-table]])
nil
user> (pprint [:a [:b:c:d [:e:f:g]:h:i:j:k]:l:m:n:o:p:q [:r:s:t:u :v:[:w [:x:y:z]]])
[:a
[:b:c:d [:e:f:g]:h:i: k]
:l
:m
:n
:o
:p
:q
[:r:s:t: u:v]
[:w [:x:y:z]]]
nil
user> (重复5 [:a:b:c:d:e:f:g(打印表格) :h:i:j])))

| :a | :c | :b | :f | :g | :d | :e | :j | :i | :h |
| -------- + -------- + -------- + -------- + -------- + -------- + -------- + -------- + -------- + -------- |
| 311650 | 311652 | 311651 | 311655 | 311656 | 311653 | 311654 | 311659 | 311658 | 311657 |
| 67627 | 67629 | 67628 | 67632 | 67633 | 67630 | 67631 | 67636 | 67635 | 67634 |
| 601726 | 601728 | 601727 | 601731 | 601732 | 601729 | 601730 | 601735 | 601734 | 601733 |
| 384887 | 384889 | 384888 | 384892 | 384893 | 384890 | 384891 | 384896 | 384895 | 384894 |
| 353946 | 353948 | 353947 | 353951 | 353952 | 353949 | 353950 | 353955 | 353954 | 353953 |
nil


I'm trying to print out my binary tree but Clojure is giving me a hard time printing out the sequences properly.

So, I have a list of nodes '(1 2 3) for example.

In each iteration I want to print out the node with a number of spaces before and after each element.

(defn spaces [n]
  (apply str (repeat n " ")))

Great, this seems to work.

So, suppose I have a list of nodes '(:a :b :c) I want to print out on one line, with as said, the spaces.

(println (map #(str (spaces before) % (spaces (dec before))) nodes))

I have a list of items. Using the map I get a list of string objects. Great, so I can print them!

But this gives me this:

(clojure.lang.LazySeq@d0b37c31 clojure.lang.LazySeq@105879a9 clojure.lang.LazySeq@8de18242)

So I googled how to print lazy sequences and I came around to using print-str command. According to the docs this prints to a string which then gets returned.

(println (print-str (map #(str (spaces before) % (spaces (dec before))) nodes)))

But this gives me this:

(clojure.lang.LazySeq@d0b37c31 clojure.lang.LazySeq@105879a9 clojure.lang.LazySeq@8de18242)

No change.. Hrm. Any help is greatly appreciated.

解决方案

user> (str (map inc (range 10)))
"clojure.lang.LazySeq@c5d38b66"
user> (pr-str (map inc (range 10)))
"(1 2 3 4 5 6 7 8 9 10)"

The toString method of LazySeq is called by str, and this avoids realizing the lazy sequence of values by opaquely showing the Object identity. The pr-str function calls the print-dup multimethod of an object, which is designed to get the version of a thing that could be understood by the reader (so for a LazySeq the literal value that would make an equal LazySeq).

For pretty formatting of structures, do check out the clojure.pprint namespace which comes with clojure.core, which has pprint, print-table, and various functions for customizing the behavior of pretty printing.

user> (require '[clojure.pprint :as pprint :refer [pprint print-table]])
nil
user> (pprint [:a [:b :c :d [:e :f :g] :h :i :j :k] :l :m :n :o :p :q [:r :s :t :u :v] [:w [:x :y :z]]])
[:a
 [:b :c :d [:e :f :g] :h :i :j :k]
 :l
 :m
 :n
 :o
 :p
 :q
 [:r :s :t :u :v]
 [:w [:x :y :z]]]
nil
user> (print-table (map #(let [start (rand-int 1e6)] (zipmap % (range start (+ start 10)))) (repeat 5 [:a :b :c :d :e :f :g :h :i :j])))

|     :a |     :c |     :b |     :f |     :g |     :d |     :e |     :j |     :i |     :h |
|--------+--------+--------+--------+--------+--------+--------+--------+--------+--------|
| 311650 | 311652 | 311651 | 311655 | 311656 | 311653 | 311654 | 311659 | 311658 | 311657 |
|  67627 |  67629 |  67628 |  67632 |  67633 |  67630 |  67631 |  67636 |  67635 |  67634 |
| 601726 | 601728 | 601727 | 601731 | 601732 | 601729 | 601730 | 601735 | 601734 | 601733 |
| 384887 | 384889 | 384888 | 384892 | 384893 | 384890 | 384891 | 384896 | 384895 | 384894 |
| 353946 | 353948 | 353947 | 353951 | 353952 | 353949 | 353950 | 353955 | 353954 | 353953 |
nil

这篇关于Clojure打印延迟序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆