Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Created January 31, 2011 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwmitchell/804092 to your computer and use it in GitHub Desktop.
Save mwmitchell/804092 to your computer and use it in GitHub Desktop.
re-map
; (re-map {:id 1 :name "this"} [:id [:label :name]])
; [:label :name] is an alias where :label is a new key
; and :name is the real kiey in the source hash.
(defn re-map [source mapping]
(into {} (map
(fn [[k,v]]
(let [mapped-key (or (some #{k} mapping) :how-to-get-items-from-aliases)]
[mapped-key v]) source))))
@ejackson
Copy link

(let [mapping {:old-k1 :new-k1
:old-k2 :old-k2
:old-k3 :new-k3}](into {} %28map %28fn [[key val]] [%28mapping key%29 val]%29
{:old-k1 1
:old-k2 2
:old-k3 3}%29))

@mwmitchell
Copy link
Author

(defn normalize-mapping [mapping](into {}
%28map %28fn [k] %28if %28vector? k%29 {%28last k%29 %28first k%29} {k k}%29%29 mapping%29))

(defn re-map [source mapping](let [n-mapping %28normalize-mapping mapping%29]
%28into {} %28map %28fn [[k,v]] [%28k n-mapping%29 v]%29 source%29%29))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment