Skip to content

Files

Latest commit

e008f91 · Mar 20, 2020

History

History
149 lines (121 loc) · 2.74 KB

maps.md

File metadata and controls

149 lines (121 loc) · 2.74 KB

How map keys are formatted

Sorted

By default, zprint will output maps with keys in sorted order.

(def map1 {:a :A :b :B :c :C :d :D :f :F :g :G :h :H :i :I :j :J :k :K :l :L :m :M})

(clojure.pprint/pprint map1)

{:m :M,
 :l :L,
 :k :K,
 :g :G,
 :c :C,
 :j :J,
 :h :H,
 :b :B,
 :d :D,
 :f :F,
 :i :I,
 :a :A}

(czprint map1)

{:a :A,
 :b :B,
 :c :C,
 :d :D,
 :f :F,
 :g :G,
 :h :H,
 :i :I,
 :j :J,
 :k :K,
 :l :L,
 :m :M}

Ordered

You can specify your own ordering of map keys with an options map:

(czprint map1 {:map {:key-order [:k :i :m]}})

{:k :K,
 :i :I,
 :m :M,
 :a :A,
 :b :B,
 :c :C,
 :d :D,
 :f :F,
 :g :G,
 :h :H,
 :j :J,
 :l :L}

Ignored

You can use :key-ignore or :key-ignore-silent to get keys elided from map output:

(czprint map1 {:map {:key-ignore-silent [:k :i :m]} :width 50})

{:a :A,
 :b :B,
 :c :C,
 :d :D,
 :f :F,
 :g :G,
 :h :H,
 :j :J,
 :l :L}

Colored

There are two additional options for maps that can be useful:

:key-color

The value of :key-color is a map which relates keys that are 'constants' to a color in which to print that key. A constant is a keyword, string, or number. This way you can have some keys formatted in a color that is different from the color in which they would normally be formatted based on their type. It can go well with :key-order [:key1 :key2 ...] which is another way to distinguish a special key. You can place some keys at the front of the map and you can also adjust their colors to meet your needs.

(czprint map1 {:map {:key-color {:k :blue}}})

; You can't see the colors because markdown loses the colors
; but you can see how to do it.

{:a :A,
 :b :B,
 :c :C,
 :d :D,
 :f :F,
 :g :G,
 :h :H,
 :i :I,
 :j :J,
 :k :K,
 :l :L,
 :m :M}
:key-value-color

The value of :key-value-color is a map which relates keys (that don't have to be constants) to a color-map which is merged into the current color-map, and is used when formatting the value of that key. This way you can have the values of some keys formatted in a color that is different from the color in which they would normally be formatted based on their type.

(czprint map1 {:map {:key-value-color {:k {:keyword :green}}}})

; You can't see the colors because markdown loses the colors
; but you can see how to do it.

{:a :A,
 :b :B,
 :c :C,
 :d :D,
 :f :F,
 :g :G,
 :h :H,
 :i :I,
 :j :J,
 :k :K,
 :l :L,
 :m :M}

You can place these option maps on individual calls. If you are operating at the REPL, using set-options! is frequently a good way to set up useful formatting. For more enduring changes, this options map can appear anywhere an options map is accepted.