[Clojure] Idiom for transforming a map – Grokbase

Something like this? (into {} (for [[k v] my-map] :when (pred k v) [(transform k) v])) ~BG Sent from phone. Please excuse brevity. -- 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 clojure+ [email protected] For more options, visit this group at you can use reduce-kv...This is exactly its purpose - to be able to reduce maps without the need of destructuring example: (reduce-kv some-fn-with-3-args {} old-map) Jim -- 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 clojure+ [email protected] For more options, obviously, you would use 'assoc' in your reducing fn...If your old-map is rather big then you can start with a transient version of a map and persist it at the end of the entire operation. new example: (peristent! (reduce-kv #(assoc! % (transform %2) %3) (transient (hash-map)) old-map)) ,,% is the initial value %2 is the key of the old-map & %3 is the value of the old-map Alex's solution is also quite idiomatic and it takes care of the transient/peristent conversion as well...It seems slightly CORRECTION: you'd be building MANY intermediate vectors...as many as your keys in the old-map... In addition, as a bonus, in case your old-map is truly massive you can partition it and reduce/fold it in parallel really easily with reducers...see this for more: http://www.thebusby.com/2012/07/tips-tricks-with-clojure-reducers.html So, normal map ->, 'reduce-kv' or 'into'/'for' big map ->, 'reduce-kv' with transients massive map ->, 'fold' (reducers) Hope that covers everything :) Jim -- You Excellent stuff, guys. Thanks for the help. I just got 'Joy of Clojure' and 'Clojure Programming' in the mail yesterday so hopefully I'll be up to speed quickly. -- 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 clojure+ [email protected] For more options, visit no worries...happy reading! :-) Jim -- 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 clojure+ [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en This kind of thing comes up a lot, so you'll find a variation of this function in a lot of projects. flatland/useful has a lot of functions like this, and is a good candidate for including as your utils library. https://github.com/flatland/useful/blob/develop/src/flatland/useful/map.clj#L54 At its most simple, you just need to do something like: (into {} (for [[k v] source-map] [(transform-fn k) v])) -- You received this message because you are subscribed to the Google Groups 'Clojure' group Source.


Яндекс.Метрика Рейтинг@Mail.ru Free Web Counter
page counter
Last Modified: January 26, 2015 @ 12:00 am