Skip to content

Commit

Permalink
CLJS-1497: find on an associative collection does not return collec…
Browse files Browse the repository at this point in the history
…tion key
  • Loading branch information
anmonteiro authored and dnolen committed Apr 7, 2017
1 parent 7950965 commit b799fb9
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
57 changes: 50 additions & 7 deletions src/main/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,10 @@
"Returns a new collection of coll with a mapping from key k to
value v added to it."))

(defprotocol IFind
"Protocol for implementing entry finding in collections."
(-find [coll k]))

(defprotocol IMap
"Protocol for adding mapping functionality to collections."
#_(-assoc-ex [coll k v])
Expand Down Expand Up @@ -2023,6 +2027,10 @@ reduces them without incurring seq initialization"
"Returns true if coll implements Associative"
[x] (satisfies? IAssociative x))

(defn ^boolean ifind?
"Returns true if coll implements IFind"
[x] (satisfies? IFind x))

(defn ^boolean sequential?
"Returns true if coll satisfies ISequential"
[x] (satisfies? ISequential x))
Expand Down Expand Up @@ -2237,7 +2245,9 @@ reduces them without incurring seq initialization"
(when (and (not (nil? coll))
(associative? coll)
(contains? coll k))
[k (get coll k)]))
(if (ifind? coll)
(-find coll k)
[k (get coll k)])))

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

IFind
(-find [coll k]
[k (get coll k)])

IVector
(-assoc-n [coll n val]
(cond
Expand Down Expand Up @@ -5438,6 +5452,10 @@ reduces them without incurring seq initialization"
(-assoc-n coll key val)
(throw (js/Error. "Subvec's key for assoc must be a number."))))

IFind
(-find [coll key]
[key (get coll key)])

IVector
(-assoc-n [coll n val]
(let [v-pos (+ start n)]
Expand Down Expand Up @@ -5954,6 +5972,10 @@ reduces them without incurring seq initialization"
true
false))

IFind
(-find [coll k]
[k (get coll k)])

IKVReduce
(-kv-reduce [coll f init]
(let [len (alength keys)]
Expand Down Expand Up @@ -6126,7 +6148,7 @@ reduces them without incurring seq initialization"
(-lastIndexOf coll x (count coll)))
(lastIndexOf [coll x start]
(-lastIndexOf coll x start))

IMeta
(-meta [coll] _meta)

Expand Down Expand Up @@ -6154,7 +6176,7 @@ reduces them without incurring seq initialization"

IHash
(-hash [coll] (hash-ordered-coll coll))

ISeq
(-first [coll]
[(aget arr i) (aget arr (inc i))])
Expand Down Expand Up @@ -6261,7 +6283,7 @@ reduces them without incurring seq initialization"
IIterable
(-iterator [this]
(PersistentArrayMapIterator. arr 0 (* cnt 2)))

ISeqable
(-seq [coll]
(persistent-array-map-seq arr 0 nil))
Expand Down Expand Up @@ -6302,6 +6324,11 @@ reduces them without incurring seq initialization"
(-contains-key? [coll k]
(not (== (array-map-index-of coll k) -1)))

IFind
(-find [coll k]
(let [idx (array-map-index-of coll k)]
[(aget arr idx) (get coll k)]))

IMap
(-dissoc [coll k]
(let [idx (array-map-index-of coll k)]
Expand Down Expand Up @@ -6472,7 +6499,7 @@ reduces them without incurring seq initialization"
tcoll)
(throw (js/Error. "dissoc! after persistent!")))))

(declare TransientHashMap PersistentHashMap)
(declare TransientHashMap)

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

(declare TransientHashMap)

(deftype HashMapIter [nil-val root-iter ^:mutable seen]
Object
(hasNext [_]
Expand Down Expand Up @@ -7301,6 +7326,12 @@ reduces them without incurring seq initialization"
:else (not (identical? (.inode-lookup root 0 (hash k) k lookup-sentinel)
lookup-sentinel))))

IFind
(-find [coll k]
(if has-nil?
[nil nil-val]
(.inode-find root 0 (hash k) k nil)))

IMap
(-dissoc [coll k]
(cond (nil? k) (if has-nil?
Expand Down Expand Up @@ -7738,6 +7769,10 @@ reduces them without incurring seq initialization"
(-assoc [node k v]
(assoc [key val] k v))

IFind
(-find [node k]
[key val])

IVector
(-assoc-n [node n v]
(-assoc-n [key val] n v))
Expand Down Expand Up @@ -7890,6 +7925,10 @@ reduces them without incurring seq initialization"
(-assoc [node k v]
(assoc [key val] k v))

IFind
(-find [node k]
[key val])

IVector
(-assoc-n [node n v]
(-assoc-n [key val] n v))
Expand Down Expand Up @@ -8128,6 +8167,10 @@ reduces them without incurring seq initialization"
(-contains-key? [coll k]
(not (nil? (.entry-at coll k))))

IFind
(-find [coll k]
(.entry-at coll k))

IMap
(-dissoc [coll k]
(let [found (array nil)
Expand Down
33 changes: 33 additions & 0 deletions src/test/cljs/cljs/collections_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,36 @@
(deftest test-cljs-1951
(is (= () (interleave)))
(is (= '(1 2 3) (interleave [1 2 3]))))

(deftest test-cljs-1497
(testing "PersistentArrayMap"
(let [metadata {:a 1}
k [1 2 3]
v 1
map (array-map (with-meta k metadata) v)
[k' v'] (find map k)]
(is (= k k'))
(is (= v v'))
(is (= metadata (meta k')))))
(testing "PersistentHashMap"
(let [metadata {:a 1}
k [1 2 3]
v 1
map (hash-map (with-meta k metadata) v)
[k' v'] (find map k)]
(is (= k k'))
(is (= v v'))
(is (= metadata (meta k'))))
(let [map (hash-map nil :foo)]
(is (= (find map nil) [nil :foo]))))
(testing "PersistentTreeMap"
(let [metadata {:a 1}
k [1 2 3]
v 1
map (sorted-map (with-meta k metadata) v)
[k' v'] (find map k)]
(is (= k k'))
(is (= v v'))
(is (= metadata (meta k'))))
(let [map (sorted-map nil :foo)]
(is (= (find map nil) [nil :foo])))))

0 comments on commit b799fb9

Please sign in to comment.