Hello,
In order to update fields in nested structures/maps easily
I have created a macro 'field-write'.
(defmacro field-write [st k v access-spec]
; st=data, k=key to update, v=val to put, access-spec=access
vector
; ... code listing after interaction
user=> nx
{:a {:b {:c {:content 10}}}}
user=> (field-write nx :content 1000 [:a :b :c])
{:a {:b {:c {:content 1000}}}}
user=> (macroexpand '(field-write nx :content 1000 [:a :b :c]))
(clojure/assoc
nx :a
(clojure/assoc
(clojure/-> nx :a) :b
(clojure/assoc
(clojure/-> nx :a :b) :c
(clojure/assoc
(clojure/-> nx :a :b :c):content 1000))))
[formatting added above for readability]
It seems to be working fine but I thought it may be
good to get inputs from the Clojure experts here to
see if there is a better way to do this in Clojure.
(def nx {:a {:b {:c {:content 10}}}})
(def field-write)
(defmacro field-write [st k v access-spec]
; st=data, k=key to update, v=val to put, access-spec=access
vector
(if (pred/empty? access-spec)
`(assoc ~st ~k ~v)
(field-write st (last access-spec)
`(assoc (-> ~st [EMAIL PROTECTED]) ~k ~v)
(butlast access-spec))))
Thanks.
Parth
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---