On Sep 30, 2010, at 2:53 AM, Baishampayan Ghose 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 }
>
> What about this -
>
> (into {} (for [[k v] { :stuff 42 :like 13 :this 7 }]
> [(.toUpperCase (name k)) v]))
>
One small suggestion based on something Christophe Grand once pointed out:
(defn string-keys [m]
(into (empty m) (for [[k v] m] [(.toUpperCase (name k)) v])))
(defn keyword-keys [m]
(into (empty m) (for [[k v] m] [(keyword (.toLowerCase k)) v])))
This will preserve the type of the map.
(string-keys { :stuff 42 :like 13 :this 7 } ) => {"THIS" 7, "LIKE" 13, "STUFF"
42}
(keyword-keys (string-keys { :stuff 42 :like 13 :this 7 } )) => {:stuff 42,
:like 13, :this 7}
Have all good days,
David Sletten
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en