fixed rename-keys to not delete entries when there is a collision #23

Closed
wants to merge 1 commit into
from
View
7 src/clj/clojure/set.clj
@@ -80,11 +80,10 @@
[map kmap]
(reduce
(fn [m [old new]]
- (if (and (not= old new)
- (contains? m old))
- (-> m (assoc new (get m old)) (dissoc old))
+ (if (contains? map old)
+ (assoc m new (get map old))
m))
- map kmap))
+ (apply dissoc map (keys kmap)) kmap))
(defn rename
"Returns a rel of the maps in xrel with the keys in kmap renamed to the vals in kmap"
View
5 test/clojure/test_clojure/clojure_set.clj
@@ -162,8 +162,9 @@
(deftest test-rename-keys
(are [x y] (= x y)
- (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
- ))
+ (set/rename-keys {:a "one" :b "two"} {:a :z}) {:z "one" :b "two"}
+ (set/rename-keys {:a "one" :b "two"} {:a :z :c :y}) {:z "one" :b "two"}
+ (set/rename-keys {:a "one" :b "two" :c "three"} {:a :b :b :a}) {:a "two" :b "one" :c "three"}))
(deftest test-index
(are [x y] (= x y)