forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pprint.mal
43 lines (35 loc) · 1.26 KB
/
pprint.mal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
;; Pretty printer a MAL object.
(def! pprint
(let* [
spaces- (fn* [indent]
(if (> indent 0)
(str " " (spaces- (- indent 1)))
""))
pp-seq- (fn* [obj indent]
(let* [xindent (+ 1 indent)]
(apply str (pp- (first obj) 0)
(map (fn* [x] (str "\n" (spaces- xindent)
(pp- x xindent)))
(rest obj)))))
pp-map- (fn* [obj indent]
(let* [ks (keys obj)
kindent (+ 1 indent)
kwidth (count (seq (str (first ks))))
vindent (+ 1 (+ kwidth kindent))]
(apply str (pp- (first ks) 0)
" "
(pp- (get obj (first ks)) 0)
(map (fn* [k] (str "\n" (spaces- kindent)
(pp- k kindent)
" "
(pp- (get obj k) vindent)))
(rest (keys obj))))))
pp- (fn* [obj indent]
(cond
(list? obj) (str "(" (pp-seq- obj indent) ")")
(vector? obj) (str "[" (pp-seq- obj indent) "]")
(map? obj) (str "{" (pp-map- obj indent) "}")
:else (pr-str obj)))
]
(fn* [obj]
(println (pp- obj 0)))))