Clojure printing lazy sequence

Christophe De Troyer picture Christophe De Troyer · May 1, 2014 · Viewed 8.7k times · Source

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.

Answer

noisesmith picture noisesmith · May 1, 2014
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