I think you can just use the update-in function like:
1:1 user=> (def m {:a {:b {:c {:d 3}}}})
#'user/m
1:2 user=> (update-in m [:a :b :c :d] - 5)
{:a {:b {:c {:d -2}}}}
On Sun, Jan 11, 2009 at 11:08 AM, CuppoJava <[email protected]>wrote:
>
> Hi,
> I'm just wondering if there's a clever way of creating a new map from
> an existing map of map of maps.. with a key deep inside altered.
>
> ie. given this map: {:a {:b {:c {:d 3}}}}
>
> i want to create a new map, with the value at :d increased by 5.
>
> I wrote a macro to do this, but it's quite ugly.
>
> Thanks for the tip
> -Patrick
>
> --------------------------------------------------------------------------
> In case this helps at all, this is the macro that I wrote.
> Usage: (set_map mymap [:a :b :c :d] (+ it 5))
>
> (defmacro -set_map [mymap mykeys expr]
> (let [syms (take (dec (count mykeys))
> (repeatedly gensym))
> bindings (interleave
> (concat syms ['it])
> (map list
> mykeys
> (concat [mymap] syms)))]
> `(let [...@bindings]
> ~((fn assoc_fn [maps keys expr]
> (if (empty? keys)
> expr
> `(assoc ~(first maps) ~(first keys)
> ~(assoc_fn (rest maps) (rest keys) expr))))
> (concat [mymap] syms) mykeys expr))))
>
> (defmacro set_map [map keys expr]
> `(let [map# ~map]
> (-set_map map# ~keys ~expr)))
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---