Skip to content

Instantly share code, notes, and snippets.

@bmabey
Created October 26, 2010 21:10
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 bmabey/647817 to your computer and use it in GitHub Desktop.
Save bmabey/647817 to your computer and use it in GitHub Desktop.
utils.clj
(defn map-keys
"Renames each key of m to the value of generated by f when given the key"
[f m]
(persistent!
(reduce (fn [m [k v]] (assoc! m (f k) v)) (transient {}) m)))
@bhenry
Copy link

bhenry commented Oct 26, 2010

what does the transient stuff bring to the table where this doesn't work?

(defn map-keys [f m] 
  (zipmap (map f (keys m)) 
          (vals m)))

@bmabey
Copy link
Author

bmabey commented Oct 26, 2010

The transient version is faster since it isn't modifying the same persistent structure the entire time. http://clojure.org/transients

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