I'm trying to figure out if there is a better / more concise / more generic 
way to write an update function. The data I have is a vector of maps 
containing vectors of maps. 
For example:
  [{:values [{:y 1 :x 4}{:y 2 :x 7}]}{:values [{:y 5 :x 8}]}]

The goal is to update all the :y values or all the :x values according to a 
passed-in function. As a concrete example, I'd like to invert the sign of 
all the :y values.

So here's the working function which I've come up with:

(defn update-nested-fields 
  "Updates a vector of maps containing vectors of maps"
  [col [k1 k2] f]
  (map (fn [outer-map] 
         (update-in outer-map 
                    [k1] 
                    (fn [inner-col] 
                      (map
                       (fn [inner-map] 
                         (update-in inner-map [k2] f))   
                       inner-col)))) 
       col))

;; invert the :y values
(update-nested-fields 
 [{:values [{:y 1 :x 4}{:y 2 :x 7}]}{:values [{:y 5 :x 8}]}]
 [:values :y]
 (partial * -1))



As you can see, at a simple level it is: (map (update-in (map (update-in 
f))).

I'm really wondering if I'm missing an obvious simplification. I've tried 
to decide if a threading macro would help, but I don't see how, since the 
outer collection is provided to the outermost map function, rather than the 
inner-most function. 







-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to