Dear Steve, I’m new in clojure as well, but if we try to break up your one function into two, we’d have the following:
(defn update1 "Given a two-level nested vector of maps, two keys for each map, and an update function. Apply the function in the inner map, and returns nested structure." [vect [k1 k2] f] (map (fn [m] (update-in m [k1] update2 k2 f)) vect)) (defn update2 "Given a vector of maps, a key for the map, and an update function. Apply the function to value of the key." [vect k f] (map (fn [item] (update-in item [k] f)) vect)) which seems easier to follow than one, though YMMV. Best, -- jv Sent with Sparrow (http://www.sparrowmailapp.com/?sig) On Monday, December 22, 2014 at 12:34 PM, Steve Ashton wrote: > 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 > (mailto: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 > (mailto: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 > (mailto:clojure+unsubscr...@googlegroups.com). > For more options, visit https://groups.google.com/d/optout. -- 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.