After writing the function I realized I could extract the lowering function and let it take a function to apply to all keys of m and all keys in m's vals.
Here is the map-keys function with the lower-key function extracted. I run it on some goofy data at the end. Try it in a REPL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn lower-key [x] | |
(if (or (string? x) (keyword? x)) | |
(-> x name .toLowerCase keyword) | |
x)) | |
(defn map-keys | |
"applies f to each key of m. also to keys of m's vals and so on." | |
[f m] | |
(zipmap | |
(map (fn [k] | |
(f k)) | |
(keys m)) | |
(map (fn [v] | |
(if (map? v) | |
(map-keys f v) | |
v)) | |
(vals m)))) | |
(map-keys lower-key | |
{:aBaB 123 | |
"CAsasas" {1 "doesn't apply to non-string non-keyword keys" | |
:yoGURt "mmMMm" | |
:notVALUES "either"}}) |
No comments:
Post a Comment