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 heavier
though cos you're building an intermediate vector.
Jim
On 09/01/13 18:13, Jim - FooBar(); wrote:
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
On 09/01/13 18:09, Jonathon McKitrick wrote:
I have a map derived from JSON data where the keys are strings. I
want to build a new map from this one, where the new keys are based
on integers extracted from the strings of the old keys by a regex.
Not all entries will be transformed into the new map.
In Common Lisp, I would LOOP and PUSH entries onto the map, but I
need to learn the Clojure way of immutability.
What's the best way to create this map?
--
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