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 clojure@googlegroups.com
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