You're viewing version 1.2.0 of rename-keys. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

rename-keys

clojure.set

  • (rename-keys map kmap)
Returns the map with the keys in kmap renamed to the vals in kmap

2 Examples top

  • user=> (rename-keys {:a 1, :b 2} {:a :new-a, :b :new-b})
    {:new-a 1, :new-b 2}
    
    
    ;; The behavior when the second map contains a key not in the first is interesting.
    ;; I suspect you shouldn't depend on it. (Clojure 1.1 - no longer happens in 1.2.1)
    
    user=> (rename-keys {:a 1} {:b :new-b})
    { :a 1, :new-b nil}
    
  • ;; You need to be careful about key collisions.  You probably shouldn't 
    ;; depend on the exact behavior.
    user=> (rename-keys {:a 1 :b 2} {:a :b})
    {:b 1}
    
    user=> (rename-keys  {:a 1 :b 2}  {:a :b :b :a})
    {:a 1}
    
    ;; You can work around key collisions by using an array-map to control
    ;; the order of the renamings.
    user=> (rename-keys  {:a 1 :b 2 :c 3}  (array-map :a :tmp :b :a :tmp :b))
    {:b 1, :a 2, :c 3}
    
Log in to add / edit an example.

See Also top

  • 0
    clojure.set/rename

    Returns a rel of the maps in xrel with the keys in kmap renamed to th

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/set.clj:77 top

(defn rename-keys
  "Returns the map with the keys in kmap renamed to the vals in kmap"
  {:added "1.0"}
  [map kmap]
    (reduce 
     (fn [m [old new]]
       (if (and (not= old new)
                (contains? m old))
         (-> m (assoc new (get m old)) (dissoc old))
         m)) 
     map kmap))
Vars in clojure.set/rename-keys: -> and assoc contains? defn dissoc fn map not= reduce get
Used in 0 other vars

Comments top

No comments for rename-keys. Log in to add a comment.