I wrote a blog recently on a helper function I use for stuff like this
called mapmap:
http://tech.puredanger.com/2010/09/24/meet-my-little-friend-mapmap/

mapmap takes a function to generate keys and a function to generate
values, applies them to a sequence, and zipmaps their results.  Using
a map as the sequence, you'd do something like:

(mapmap #(upper-case (key %)) identity m)

If you wanted to upper-case the values, mapmap uses identity as a
default key function, so you'd do:

(mapmap #(upper-case (val %)) m)

Someone suggested on twitter that a helper function over it
specifically for working from an existing map and splitting the key
and val might be nicer.  mapmap on map would of course be:

(defn mapmapmap [kf vf m]
    (mapmap (comp kf key) (comp vf val) m))

Then you could use the cleaner form for your needs:

> (mapmapmap upper-case identity { "abc" "def" "ghi" "jkl" })
{"GHI" "jkl", "ABC" "def"}

>From a readability perspective, I think that's nice.  Feel free to
throw plenty of rocks at the function names and impl though. :)

On Sep 30, 1:44 am, Sean Corfield <seancorfi...@gmail.com> wrote:
> I have a need to convert maps in the following ways:
>
> Given a map with keyword keys, I need a map with uppercase string keys
> - and vice versa.
>
> { :stuff 42 :like 13 :this 7 } <=> { "STUFF" 42 "LIKE" 13 "THIS" 7 }
>
> I've come up with various functions to do this but so far they all
> feel a bit clunky.
>
> Any suggestions for the simplest, most idiomatic solution?
>
> Here's one pair of functions I came up with...
>
> (defn- to-struct [r] (apply hash-map (flatten (map (fn [[k v]]
> [(s/upper-case (name k)) v]) r))))
>
> (defn- to-rec [m] (apply hash-map (flatten (map (fn [[k v]] [(keyword
> (s/lower-case k)) v]) m))))
>
> s is clojure.string:
>   (:use [clojure.string :as s :only (lower-case upper-case)])
>
> I came up with some using assoc and/or dissoc as well... didn't like
> those much either :)
> --
> Sean A Corfield -- (904) 302-SEAN
> Railo Technologies, Inc. --http://getrailo.com/
> An Architect's View --http://corfield.org/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to