Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experiment in compact pretty printing
=> (doseq [w (range 32)]
(prn 'width w)
(render (best-layout (spans '(1 2 3 4 (a b c) 5 6)) w))
(newline))
width 0
width 1
width 2
width 3
width 4
(1 2
3 4
(a
b
c)
5
6)
width 5
(1 2
3 4
(a b
c)
5 6)
width 6
(1 2
3 4
(a b
c)
5 6)
width 7
(1 2 3
4 (a b
c)
5 6)
width 8
(1 2 3 4
(a b c)
5 6)
width 9
(1 2 3 4
(a b c)
5 6)
width 10
(1 2 3 4
(a b c)
5 6)
width 11
(1 2 3 4
(a b c)
5 6)
width 12
(1 2 3 4
(a b c)
5 6)
width 13
(1 2 3 4
(a b c) 5 6)
width 14
(1 2 3 4
(a b c) 5 6)
width 15
(1 2 3 4
(a b c) 5 6)
width 16
(1 2 3 4
(a b c) 5 6)
width 17
(1 2 3 4
(a b c) 5 6)
width 18
(1 2 3 4
(a b c) 5 6)
width 19
(1 2 3 4
(a b c) 5 6)
width 20
(1 2 3 4
(a b c) 5 6)
width 21
(1 2 3 4 (a b c) 5 6)
width 22
(1 2 3 4 (a b c) 5 6)
width 23
(1 2 3 4 (a b c) 5 6)
width 24
(1 2 3 4 (a b c) 5 6)
width 25
(1 2 3 4 (a b c) 5 6)
width 26
(1 2 3 4 (a b c) 5 6)
width 27
(1 2 3 4 (a b c) 5 6)
width 28
(1 2 3 4 (a b c) 5 6)
width 29
(1 2 3 4 (a b c) 5 6)
width 30
(1 2 3 4 (a b c) 5 6)
width 31
(1 2 3 4 (a b c) 5 6)
@kkinnear

This comment has been minimized.

Copy link

commented Nov 22, 2017

zprint tries to make code or data compact. Zprint calls what you are doing here "wrap?", and it only does it for vectors, not lists. Because lists are often code, I didn't implement wrapping for lists, though it would not be difficult to do so. You can configure this behavior for vectors on or off. Below is what it does with your example as a vector. It makes slightly different decisions than your code, in part because it will never fail to print what you give it, even if it doesn't fit on the line.

Zprint has a huge readme, but if you load it up, you can type (czprint nil :help) and at least get started that way, should you be interested in exploring it.

zprint.core=> (doseq [i (range 31)] (println "\nwidth" i) (zprint '[1 2 3 4 [a b c] 5 6] i))

width 0
[1
 2
 3
 4
 [a
  b
  c]
 5
 6]

width 1
[1
 2
 3
 4
 [a
  b
  c]
 5
 6]

width 2
[1
 2
 3
 4
 [a
  b
  c]
 5
 6]

width 3
[1
 2
 3
 4
 [a
  b
  c]
 5
 6]

width 4
[1 2
 3 4
 [a
  b
  c]
 5
 6]

width 5
[1 2
 3 4
 [a b
  c]
 5 6]

width 6
[1 2 3
 4
 [a b
  c] 5
 6]

width 7
[1 2 3
 4
 [a b
  c] 5
 6]

width 8
[1 2 3 4
 [a b c]
 5 6]

width 9
[1 2 3 4
 [a b c]
 5 6]

width 10
[1 2 3 4
 [a b c] 5
 6]

width 11
[1 2 3 4
 [a b c] 5
 6]

width 12
[1 2 3 4
 [a b c] 5
 6]

width 13
[1 2 3 4
 [a b c] 5 6]

width 14
[1 2 3 4
 [a b c] 5 6]

width 15
[1 2 3 4
 [a b c] 5 6]

width 16
[1 2 3 4 [a b c]
 5 6]

width 17
[1 2 3 4 [a b c]
 5 6]

width 18
[1 2 3 4 [a b c] 5
 6]

width 19
[1 2 3 4 [a b c] 5
 6]

width 20
[1 2 3 4 [a b c] 5
 6]

width 21
[1 2 3 4 [a b c] 5 6]

width 22
[1 2 3 4 [a b c] 5 6]

width 23
[1 2 3 4 [a b c] 5 6]

width 24
[1 2 3 4 [a b c] 5 6]

width 25
[1 2 3 4 [a b c] 5 6]

width 26
[1 2 3 4 [a b c] 5 6]

width 27
[1 2 3 4 [a b c] 5 6]

width 28
[1 2 3 4 [a b c] 5 6]

width 29
[1 2 3 4 [a b c] 5 6]

width 30
[1 2 3 4 [a b c] 5 6]
nil
@cgrand

This comment has been minimized.

Copy link
Owner Author

commented Nov 23, 2017

@kkinnear, thanks I wasn't aware of zprint!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.