Skip to content

Commit b799fb9

Browse files
anmonteirodnolen
authored and
dnolen
committed
CLJS-1497: find on an associative collection does not return collection key
1 parent 7950965 commit b799fb9

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

src/main/cljs/cljs/core.cljs

+50-7
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@
546546
"Returns a new collection of coll with a mapping from key k to
547547
value v added to it."))
548548

549+
(defprotocol IFind
550+
"Protocol for implementing entry finding in collections."
551+
(-find [coll k]))
552+
549553
(defprotocol IMap
550554
"Protocol for adding mapping functionality to collections."
551555
#_(-assoc-ex [coll k v])
@@ -2023,6 +2027,10 @@ reduces them without incurring seq initialization"
20232027
"Returns true if coll implements Associative"
20242028
[x] (satisfies? IAssociative x))
20252029

2030+
(defn ^boolean ifind?
2031+
"Returns true if coll implements IFind"
2032+
[x] (satisfies? IFind x))
2033+
20262034
(defn ^boolean sequential?
20272035
"Returns true if coll satisfies ISequential"
20282036
[x] (satisfies? ISequential x))
@@ -2237,7 +2245,9 @@ reduces them without incurring seq initialization"
22372245
(when (and (not (nil? coll))
22382246
(associative? coll)
22392247
(contains? coll k))
2240-
[k (get coll k)]))
2248+
(if (ifind? coll)
2249+
(-find coll k)
2250+
[k (get coll k)])))
22412251

22422252
(defn ^boolean distinct?
22432253
"Returns true if no two of the arguments are ="
@@ -5152,6 +5162,10 @@ reduces them without incurring seq initialization"
51525162
(-assoc-n coll k v)
51535163
(throw (js/Error. "Vector's key for assoc must be a number."))))
51545164

5165+
IFind
5166+
(-find [coll k]
5167+
[k (get coll k)])
5168+
51555169
IVector
51565170
(-assoc-n [coll n val]
51575171
(cond
@@ -5438,6 +5452,10 @@ reduces them without incurring seq initialization"
54385452
(-assoc-n coll key val)
54395453
(throw (js/Error. "Subvec's key for assoc must be a number."))))
54405454

5455+
IFind
5456+
(-find [coll key]
5457+
[key (get coll key)])
5458+
54415459
IVector
54425460
(-assoc-n [coll n val]
54435461
(let [v-pos (+ start n)]
@@ -5954,6 +5972,10 @@ reduces them without incurring seq initialization"
59545972
true
59555973
false))
59565974

5975+
IFind
5976+
(-find [coll k]
5977+
[k (get coll k)])
5978+
59575979
IKVReduce
59585980
(-kv-reduce [coll f init]
59595981
(let [len (alength keys)]
@@ -6126,7 +6148,7 @@ reduces them without incurring seq initialization"
61266148
(-lastIndexOf coll x (count coll)))
61276149
(lastIndexOf [coll x start]
61286150
(-lastIndexOf coll x start))
6129-
6151+
61306152
IMeta
61316153
(-meta [coll] _meta)
61326154

@@ -6154,7 +6176,7 @@ reduces them without incurring seq initialization"
61546176

61556177
IHash
61566178
(-hash [coll] (hash-ordered-coll coll))
6157-
6179+
61586180
ISeq
61596181
(-first [coll]
61606182
[(aget arr i) (aget arr (inc i))])
@@ -6261,7 +6283,7 @@ reduces them without incurring seq initialization"
62616283
IIterable
62626284
(-iterator [this]
62636285
(PersistentArrayMapIterator. arr 0 (* cnt 2)))
6264-
6286+
62656287
ISeqable
62666288
(-seq [coll]
62676289
(persistent-array-map-seq arr 0 nil))
@@ -6302,6 +6324,11 @@ reduces them without incurring seq initialization"
63026324
(-contains-key? [coll k]
63036325
(not (== (array-map-index-of coll k) -1)))
63046326

6327+
IFind
6328+
(-find [coll k]
6329+
(let [idx (array-map-index-of coll k)]
6330+
[(aget arr idx) (get coll k)]))
6331+
63056332
IMap
63066333
(-dissoc [coll k]
63076334
(let [idx (array-map-index-of coll k)]
@@ -6472,7 +6499,7 @@ reduces them without incurring seq initialization"
64726499
tcoll)
64736500
(throw (js/Error. "dissoc! after persistent!")))))
64746501

6475-
(declare TransientHashMap PersistentHashMap)
6502+
(declare TransientHashMap)
64766503

64776504
(defn- array->transient-hash-map [len arr]
64786505
(loop [out (transient (.-EMPTY PersistentHashMap))
@@ -7184,8 +7211,6 @@ reduces them without incurring seq initialization"
71847211
(recur (inc j))))))
71857212
(ArrayNodeSeq. meta nodes i s nil))))
71867213

7187-
(declare TransientHashMap)
7188-
71897214
(deftype HashMapIter [nil-val root-iter ^:mutable seen]
71907215
Object
71917216
(hasNext [_]
@@ -7301,6 +7326,12 @@ reduces them without incurring seq initialization"
73017326
:else (not (identical? (.inode-lookup root 0 (hash k) k lookup-sentinel)
73027327
lookup-sentinel))))
73037328

7329+
IFind
7330+
(-find [coll k]
7331+
(if has-nil?
7332+
[nil nil-val]
7333+
(.inode-find root 0 (hash k) k nil)))
7334+
73047335
IMap
73057336
(-dissoc [coll k]
73067337
(cond (nil? k) (if has-nil?
@@ -7738,6 +7769,10 @@ reduces them without incurring seq initialization"
77387769
(-assoc [node k v]
77397770
(assoc [key val] k v))
77407771

7772+
IFind
7773+
(-find [node k]
7774+
[key val])
7775+
77417776
IVector
77427777
(-assoc-n [node n v]
77437778
(-assoc-n [key val] n v))
@@ -7890,6 +7925,10 @@ reduces them without incurring seq initialization"
78907925
(-assoc [node k v]
78917926
(assoc [key val] k v))
78927927

7928+
IFind
7929+
(-find [node k]
7930+
[key val])
7931+
78937932
IVector
78947933
(-assoc-n [node n v]
78957934
(-assoc-n [key val] n v))
@@ -8128,6 +8167,10 @@ reduces them without incurring seq initialization"
81288167
(-contains-key? [coll k]
81298168
(not (nil? (.entry-at coll k))))
81308169

8170+
IFind
8171+
(-find [coll k]
8172+
(.entry-at coll k))
8173+
81318174
IMap
81328175
(-dissoc [coll k]
81338176
(let [found (array nil)

src/test/cljs/cljs/collections_test.cljs

+33
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,36 @@
619619
(deftest test-cljs-1951
620620
(is (= () (interleave)))
621621
(is (= '(1 2 3) (interleave [1 2 3]))))
622+
623+
(deftest test-cljs-1497
624+
(testing "PersistentArrayMap"
625+
(let [metadata {:a 1}
626+
k [1 2 3]
627+
v 1
628+
map (array-map (with-meta k metadata) v)
629+
[k' v'] (find map k)]
630+
(is (= k k'))
631+
(is (= v v'))
632+
(is (= metadata (meta k')))))
633+
(testing "PersistentHashMap"
634+
(let [metadata {:a 1}
635+
k [1 2 3]
636+
v 1
637+
map (hash-map (with-meta k metadata) v)
638+
[k' v'] (find map k)]
639+
(is (= k k'))
640+
(is (= v v'))
641+
(is (= metadata (meta k'))))
642+
(let [map (hash-map nil :foo)]
643+
(is (= (find map nil) [nil :foo]))))
644+
(testing "PersistentTreeMap"
645+
(let [metadata {:a 1}
646+
k [1 2 3]
647+
v 1
648+
map (sorted-map (with-meta k metadata) v)
649+
[k' v'] (find map k)]
650+
(is (= k k'))
651+
(is (= v v'))
652+
(is (= metadata (meta k'))))
653+
(let [map (sorted-map nil :foo)]
654+
(is (= (find map nil) [nil :foo])))))

0 commit comments

Comments
 (0)