Clojure: Rename and select keys from map with one iteration -
in clojure, want both rename-keys , select-keys map. simple way of doing with:
(-> m (rename-keys new-names) (select-keys (vals new-names)))
but itetate on entire map twice. there way 1 iteration?
sure, there way single iteration.
you using reduce-kv
function:
(reduce-kv #(assoc %1 %3 (get m %2)) {} new-names)
or loop:
(into {} (for [[k v] new-names] [v (get m k)]))
if want simple piece of code, use fmap
function algo.generic
library:
(fmap m (map-invert new-names))
Comments
Post a Comment