Clojure 打印懒惰序列 [英] Clojure printing lazy sequence

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

问题描述

我正在尝试打印我的二叉树,但 Clojure 让我很难正确打印序列.

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

所以,我有一个节点列表 '(1 2 3) 例如.

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

太好了,这似乎有效.

所以,假设我有一个 nodes 的列表 '(:a :b :c) 我想在一行上打印出来,如上所述,空格.

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!

但这给了我这个:

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

所以我在谷歌上搜索了如何打印惰性序列,然后我开始使用 print-str 命令.根据文档,这会打印到一个字符串,然后返回.

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

但这给了我这个:

(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)"

LazySeqtoString 方法被str 调用,通过不透明地显示Object 标识来避免实现值的惰性序列.pr-str 函数调用对象的 print-dup 多方法,该方法旨在获取读者可以理解的事物的版本(因此对于LazySeq 使 LazySeq 相等的字面值.

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

对于结构的漂亮格式,请检查 clojure.pprint 命名空间,它带有 clojure.core,它有 pprintprint-table,以及各种自定义漂亮打印行为的函数.

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天全站免登陆