-
Notifications
You must be signed in to change notification settings - Fork 40
/
map.cljs
202 lines (159 loc) · 4.58 KB
/
map.cljs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
(ns flatland.ordered.map)
(declare equiv-impl)
(defn print-ordered-map [writer kvs ks opts]
(pr-sequential-writer
writer
(fn [k w opts]
(-write w \[)
(-write w (pr-str k))
(-write w \space)
(-write w (pr-str (get kvs k)))
(-write w \]))
"(" " " ")"
opts
ks))
(deftype OrderedMap [kvs ks]
Object
(toString [this] (pr-str* this))
(equiv [this that] (equiv-impl kvs that))
;; js/map interface
(keys [this] (es6-iterator ks))
(entries [this] (es6-entries-iterator (seq kvs)))
(values [this] (es6-iterator (vals kvs)))
(has [this k] (not (nil? (.get kvs k))))
(get [this k] (.get kvs k))
(forEach [this f]
(doseq [k ks]
(f k (get kvs k) this)))
(forEach [this f use-as-this]
(doseq [k ks]
(.call f use-as-this k (get kvs k) this)))
;; js fallbacks
(key_set [this] (to-array (keys kvs)))
(entry_set [this] (to-array (map to-array kvs)))
(value_set [this] (to-array (map val kvs)))
ICloneable
(-clone [_] (OrderedMap. kvs ks))
;; IIterable
;; (-iterator [_] )
IWithMeta
(-with-meta [this new-meta]
(if (identical? (meta kvs) new-meta)
this
(OrderedMap. (with-meta kvs new-meta) ks)))
IMeta
(-meta [this] (meta kvs))
ICollection
(-conj [coll entry]
(if (vector? entry)
(OrderedMap. (conj kvs entry) (if (contains? kvs (-nth entry 0))
ks
(conj ks (-nth entry 0))))
(OrderedMap. (conj kvs entry) (into ks
(comp (map #(-nth % 0))
(remove #(contains? kvs %)))
entry))))
IEmptyableCollection
(-empty [this]
(if (seq ks)
(OrderedMap. (-empty kvs) [])
this))
IEquiv
(-equiv [this that] (equiv-impl kvs that))
IHash
(-hash [_] (hash kvs))
ISeqable
(-seq [this]
(when (seq ks)
(map #(-find kvs %) ks)))
IReversible
(-rseq [this]
(when (seq ks)
(map #(-find kvs %) (rseq ks))))
ICounted
(-count [this] (count kvs))
ILookup
(-lookup [this attr] (-lookup kvs attr))
(-lookup [this attr not-found] (-lookup kvs attr not-found))
IAssociative
(-assoc [coll k v]
(OrderedMap. (assoc kvs k v) (if (contains? kvs k)
ks
(conj ks k))))
(-contains-key? [this k]
(contains? kvs k))
IFind
(-find [this k]
(-find kvs k))
IMap
(-dissoc [this k]
(if (contains? kvs k)
(OrderedMap. (dissoc kvs k) (into [] (remove #{k}) ks))
this))
IKVReduce
(-kv-reduce [coll f init]
(reduce
(fn [acc k]
(f acc k (get kvs k)))
init
ks))
IFn
(-invoke [this k] (kvs k))
(-invoke [this k not-found] (kvs k not-found))
IPrintWithWriter
(-pr-writer [_ writer opts]
(-write writer "#ordered/map ")
(print-ordered-map writer kvs ks opts)))
(defn equiv-impl [kvs that]
(= kvs (if (instance? OrderedMap that)
(.-kvs that)
that)))
(def ^:private empty-ordered-map (OrderedMap. {} []))
(defn ordered-map
([]
empty-ordered-map)
([coll]
(into empty-ordered-map coll))
([k v & kvs]
(apply assoc empty-ordered-map k v kvs)))
(comment
(ordered-map :foo 123 :bar 456)
;; => #ordered/map [:foo 123, :bar 456]
(conj (ordered-map :foo 123 :bar 456) [:baz 123])
;; => #ordered/map [:foo 123, :bar 456, :baz 123]
(assoc (ordered-map :foo 123 :bar 456)
:baz 123
:baq 999)
;; => #ordered/map [:foo 123, :bar 456, :baz 123, :baq 999]
(merge (ordered-map :foo 123 :bar 456)
{:baz 123
:baq 999})
;; => #ordered/map [:foo 123, :bar 456, :baz 123, :baq 999]
(= (ordered-map :foo 123 :bar 456 :baz 123)
{:foo 123 :bar 456 :baz 123})
;; => true
(= {:foo 123 :bar 456 :baz 123}
(ordered-map :foo 123 :bar 456 :baz 123))
;; => true
(map? (ordered-map :foo 123 :bar 456 :baz 123))
;; => true
(empty (ordered-map :foo 123 :bar 456 :baz 123))
;; => #ordered/map []
(ordered-map)
;; => #ordered/map []
(seq (ordered-map))
;; => nil
(reduce conj [] (ordered-map :foo 123 :bar 456 :baz 123))
;; => [[:foo 123] [:bar 456] [:baz 123]]
(keys (ordered-map :foo 123 :bar 456 :baz 123))
;; => (:foo :bar :baz)
(vals (ordered-map :foo 123 :bar 456 :baz 789))
;; => (123 456 789)
(meta (with-meta (ordered-map) {:foo :bar}))
;; => {:foo :bar}
(-> (ordered-map)
(assoc-in [:foo :bar] 1)
(assoc-in [:foo :baz] 2))
(into (ordered-map) [[:foo 1] [:bar 2] [:foo 3]])
;; #ordered/map [:foo 3, :bar 2]
)