Rowdy,

> Do I really have to assoc all the way through to the inner map? Isn't
> there a more succinct way to write this? In Common Lisp there's (setf
> (getf ...))

Remember, Clojure's data structures are immutable. You're not adding a  
value to a set -- what you're describing is (in Clojure) actually  
constructing a *new* set sharing the contents of the old, and putting  
it in three *new* nested maps (which might share contents with the  
existing maps), then setting the ref to point to the new outermost map.

You'll find that after doing this the old values of each map and the  
set still exist, unchanged. If you no longer point to them, parts will  
be GCed.

For this reason, you can't just walk the structures to the one you  
want to modify, then make the change.

I'd probably write this as:

(defn alter-it [new-member]
(dosync
   (let [m @ref-map-map-map-set
         new-set
         (conj ((comp :key3 :key2 :key1) m)
               new-member)]
     (ref-set ref-map-map-map-set
              (assoc m :key1
                (assoc (:key1 m) :key2
                  (assoc (:key2 m) :key3 new-set)))))))


then get disgusted and write a macro to do it :)

However, I'd point out that:

* This might be a sign that you're doing things 'wrong'.

* You could always use a ref in your map instead of a concrete set --  
this basically gives you mutability. E.g.,

(def inner-set (ref #{}))
(def ref-map-map-map-set (ref {:key1 {:key2 {:key3 inner-set}}}))
(println @ref-map-map-map-set)
(dosync (ref-set inner-set (conj @inner-set :foo-bar)))
(println @ref-map-map-map-set)

Hope that helps,

-R

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to