Skip to content

Commit 48dd946

Browse files
committed
wip
1 parent 6cc60fc commit 48dd946

File tree

1 file changed

+55
-50
lines changed

1 file changed

+55
-50
lines changed

src/nextjournal/clerk/always_array_map.clj

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns nextjournal.clerk.always-array-map
22
"A persistent data structure that is based on array-map, but doesn't turn into a hash-map by using assoc etc.
3-
Prints like a normal Clojure map in the order of insertion.")
3+
Prints like a normal Clojure map in the order of insertion."
4+
(:require [nextjournal.clerk.utils :as utils]))
45

56
(set! *warn-on-reflection* true)
67

@@ -9,72 +10,76 @@
910

1011
(declare ->AlwaysArrayMap)
1112

12-
(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
13-
clojure.lang.ILookup
14-
(valAt [_ k]
15-
(get the-map k))
13+
(utils/if-bb
14+
nil
15+
(deftype AlwaysArrayMap [^clojure.lang.PersistentArrayMap the-map]
16+
clojure.lang.ILookup
17+
(valAt [_ k]
18+
(get the-map k))
1619

17-
clojure.lang.Seqable
18-
(seq [_]
19-
(seq the-map))
20+
clojure.lang.Seqable
21+
(seq [_]
22+
(seq the-map))
2023

21-
clojure.lang.IPersistentMap
22-
(assoc [_ k v]
23-
(if (< (count the-map) 8)
24-
(->AlwaysArrayMap (assoc the-map k v))
25-
(->AlwaysArrayMap (assoc-after the-map k v))))
24+
clojure.lang.IPersistentMap
25+
(assoc [_ k v]
26+
(if (< (count the-map) 8)
27+
(->AlwaysArrayMap (assoc the-map k v))
28+
(->AlwaysArrayMap (assoc-after the-map k v))))
2629

27-
(assocEx [_ _k _v]
28-
(throw (ex-info "Not implemented" {})))
30+
(assocEx [_ _k _v]
31+
(throw (ex-info "Not implemented" {})))
2932

30-
(without [_ k]
31-
(->AlwaysArrayMap (dissoc the-map k)))
33+
(without [_ k]
34+
(->AlwaysArrayMap (dissoc the-map k)))
3235

33-
clojure.lang.Associative
34-
(containsKey [_ k]
35-
(contains? the-map k))
36+
clojure.lang.Associative
37+
(containsKey [_ k]
38+
(contains? the-map k))
3639

37-
clojure.lang.IPersistentCollection
38-
(equiv [_ other]
39-
(= the-map other))
40-
(count [_]
41-
(count the-map))
40+
clojure.lang.IPersistentCollection
41+
(equiv [_ other]
42+
(= the-map other))
43+
(count [_]
44+
(count the-map))
4245

43-
java.lang.Iterable
44-
(iterator [_]
45-
(.iterator the-map))
46+
java.lang.Iterable
47+
(iterator [_]
48+
(.iterator the-map))
4649

47-
clojure.lang.IMeta
48-
(meta [_]
49-
(meta the-map))
50+
clojure.lang.IMeta
51+
(meta [_]
52+
(meta the-map))
5053

51-
clojure.lang.IObj
52-
(withMeta [_ meta]
53-
(->AlwaysArrayMap (with-meta the-map meta)))
54+
clojure.lang.IObj
55+
(withMeta [_ meta]
56+
(->AlwaysArrayMap (with-meta the-map meta)))
5457

55-
Object
56-
(toString [_]
57-
"<always-array-map>"))
58+
Object
59+
(toString [_]
60+
"<always-array-map>")))
5861

5962
(defn assoc-before [aam k v]
6063
(->AlwaysArrayMap (apply array-map (list* k v (interleave (keys aam) (vals aam))))))
6164

6265
(defn always-array-map [& kvs]
6366
(->AlwaysArrayMap (apply array-map kvs)))
6467

65-
(defmethod print-method AlwaysArrayMap
66-
[v ^java.io.Writer writer]
67-
(.write writer "{")
68-
(let [write-kv! (fn [k v]
69-
(.write writer (pr-str k))
70-
(.write writer " ")
71-
(.write writer (pr-str v)))]
72-
(doseq [[k v] (butlast v)]
73-
(write-kv! k v)
74-
(.write writer ", "))
75-
(let [[k v] (last v)]
76-
(write-kv! k v)))
77-
(.write writer "}"))
68+
(utils/if-bb
69+
nil
70+
(defmethod print-method AlwaysArrayMap
71+
[v ^java.io.Writer writer]
72+
(.write writer "{")
73+
(let [write-kv! (fn [k v]
74+
(.write writer (pr-str k))
75+
(.write writer " ")
76+
(.write writer (pr-str v)))]
77+
(doseq [[k v] (butlast v)]
78+
(write-kv! k v)
79+
(.write writer ", "))
80+
(let [[k v] (last v)]
81+
(write-kv! k v)))
82+
(.write writer "}")))
7883

7984
(comment
8085
(pr-str (always-array-map 1 2))

0 commit comments

Comments
 (0)