Skip to content

Commit c89bf2e

Browse files
l0st3dstuarthalloway
authored andcommitted
fixed rename-keys to not delete entries when there is a collision
Signed-off-by: Stuart Halloway <[email protected]>
1 parent f3e25bb commit c89bf2e

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/clj/clojure/set.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@
8080
[map kmap]
8181
(reduce
8282
(fn [m [old new]]
83-
(if (and (not= old new)
84-
(contains? m old))
85-
(-> m (assoc new (get m old)) (dissoc old))
83+
(if (contains? map old)
84+
(assoc m new (get map old))
8685
m))
87-
map kmap))
86+
(apply dissoc map (keys kmap)) kmap))
8887

8988
(defn rename
9089
"Returns a rel of the maps in xrel with the keys in kmap renamed to the vals in kmap"

test/clojure/test_clojure/clojure_set.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@
162162

163163
(deftest test-rename-keys
164164
(are [x y] (= x y)
165-
(set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
166-
))
165+
(set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
166+
(set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}
167+
(set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a}) {:a "two" :b "one" :c "three"}))
167168

168169
(deftest test-index
169170
(are [x y] (= x y)

0 commit comments

Comments
 (0)