Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure printing lazy sequence

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.

like image 476
Christophe De Troyer Avatar asked May 01 '14 10:05

Christophe De Troyer


People also ask

What is a lazy seq in Clojure?

clojure. core/lazy-seq accepts one or more forms that produce a sequence of nil (when the sequence is fully realized) and returns a seqable data structure that invokes the body the first time the value is needed and then caches the result.

What is a lazy sequence?

Lazy sequences are regular sequences where each item is computed on demand rather than up front. For example, consider this array of numbers: let numbers = Array(1... 100000) That will hold 100,000 numbers.

What is a sequence in Clojure?

Sequences are the key abstraction that connects two of the most important parts of Clojure - immutable persistent collections and the sequence library. Lisp has a deep tradition of list-oriented transformation functions.


1 Answers

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
like image 84
noisesmith Avatar answered Sep 22 '22 17:09

noisesmith