2016-05-06 8 views
2

Gibt es eine bequeme Möglichkeit in ClojureScript, eine verschachtelte Hash-Map so auszudrucken, dass die gesamte Baumstruktur sofort sichtbar wird.Pretty-print Verschachtelte Hash-Map in ClojureScript

{:a {:b 1 
    :c 9} 
:b {:d 8 
    :e {:f 2 
     :g 3 
     :h 4}} 
:c 10} 

EDIT:

Zum Beispiel eine Karte wie diese

(def my-map {:a {:b 1 :c 9} :b {:d 8 :e {:f 2 :g 3 :h 4}} :c 10}) 

sollte wie folgt gedruckt werden Es könnte auch Vektoren in der Karte sein. Der Usecase dient nur dazu, größere Datenstrukturen während der Entwicklung zu inspizieren.

Antwort

1

Es gibt keine integrierte Möglichkeit, dies zu tun. Sie könnten Ihrem Wunsch nahe kommen, indem Sie cljs.pprint verwenden undauf einen niedrigen Wert setzen.

ich einen Blick auf eine kleine Bibliothek nehmen würde empfehlen shodan, die eine sehr nützliche inspect Funktion bietet:

(require '[shodan.inspection :refer [inspect]]) 

(inspect {:aaaaaa 1 
      :bbbbbb {:ccc 2 
        :dddddd [1 2 3 4 5]}}) 

Es wird nichts in Ihrem CLJS REPL drucken, sondern wird eine praktische Ansicht in Ihrem Browser zur Verfügung stellen Konsole:

enter image description here

Sie kollabieren und verschachtelte Datenstrukturen erweitern kann - es ist im Grunde tut, was Sie gefragt. Ich

+0

netter Weg, aber wenn die Ausgabe 10 überschreitet, wird es ziemlich hässlich sein: '(def my-map {: a {: b 1: c 9}: b {: d 8: e {: f 2: g 3: h 4}}: c "sssssssssssssssssss"}) ' – leetwinski

+1

Ja, Sie haben Recht. Kürzlich habe ich eine kleine Bibliothek entdeckt, die in solchen Fällen gute Arbeit leistet. Ich habe der Antwort Informationen darüber hinzugefügt. –

0

Als persönliche Herausforderung schrieb den folgenden Code:

(enable-console-print!) 
(def atomic? (complement coll?)) 
(def padding #(apply str (repeat % " "))) 
(def tabulate #(apply str (repeat % "\t"))) 
(def strcat #(->> (apply concat %&) (apply str))) 
(defn my-max-key [x] (if (empty? x) [""] (apply (partial max-key count) x))) 
(defn longest-key [m] (->> m keys (filter atomic?) (map str) my-max-key)) 
(def length (comp count str)) 
(def not-map? (complement map?)) 
(def nested? #(some coll? %)) 
(def join #(apply str (interpose % %2))) 
(def join-lines (partial join "\n")) 
(defn has-atomic? [coll] (some atomic? coll)) 
(defn diff-key-lengths [key1 key2] (- (length key1) (length key2))) 
(defn convert 
    ([thing] (convert -1 thing)) 
    ([depth thing] 
    (defn convert-items [] 
    (defn convert-seq [] 
     (conj [] 
      (map (partial convert (inc depth)) thing) 
      "")) 
    (defn string-horizontally [[key value]] 
     (str (tabulate (inc depth)) 
      key 
      (padding (diff-key-lengths (longest-key thing) key)) 
      " → " 
      value)) 
    (defn string-vertically [[key value]] 
     (str (convert (inc depth) key) "\n" 
      (convert (+ 2 depth) "↓") "\n" 
      (convert (inc depth) value) "\n")) 
    (defn convert-kv [[key value]] 
     (if (nested? [key value]) 
      (string-vertically [key value]) 
      (string-horizontally [key value]))) 
    (cond (atomic? thing) 
      [(str (tabulate depth) thing)] 

      (not-map? thing) 
      (convert-seq) 

      (map? thing) 
      (map convert-kv thing))) 
    (->> (convert-items) flatten join-lines))) 

(def sample-input [["the first thing in this nested vector"] 
        {{"this is a key in a nested map" 
        "that points to me!!!"} 
        {"and that entire map points to this map!!!" 
        "cool!!!" 
        "but it gets cooler cause..." 
        "the value's line up!!!"}}]) 
(->> sample-input convert println) 

Das Terminal ausgegeben (psst ... die Werte in einer Karte überlagern sich, aber ich glaube nicht, dass Chrom eine dicktengleiche Schriftart verwendet !): chrome screenshot of index.html