Note: to repeatedly assoc-in over a seq of indices you'd want (reduce #(assoc-in %1 [%2 :c] 50) input-vec index-seq)
Replace the key :c and value 50 as needed. If the 50 would actually be computed from the index, an expression using %2 would work. If the 50 would actually be computed from the existing value associated with the :c (or whatever) key, use update-in instead of assoc-in and pass in the function that transforms the existing value to the appropriate new value as the last argument to update-in. On Tue, Jul 30, 2013 at 6:27 PM, Cedric Greevey <cgree...@gmail.com> wrote: > That's basically the same as the first solution, in terms of how it works > under the hood. > > If the desire is for the output to share structure with the input, not > just the maps but the vector as well, then you need to do something a bit > more complicated. First you need to find the map you want, and return its > index in the vector (assuming it will be unique if present). Then you need > to use assoc-in with the index, like (assoc-in v [idx :c] 50) or whatever. > > If there may be more than one map that should be adjusted, you could > return a seq of indices and repeatedly assoc-in, but I doubt this would > give superior efficiency unless the vector was typically huge and the > number of maps needing changes typically much smaller. With million-element > vectors and two or three maps needing updating it would likely be > preferable to the (vec (for ...)) approach. With dozen-element vectors with > half a dozen maps needing updating I'd go with (vec (for ...)). > > > > On Tue, Jul 30, 2013 at 4:58 PM, Jim - FooBar(); <jimpil1...@gmail.com>wrote: > >> Hi, >> >> how about .... >> >> (vec >> (for [{:keys [a b c] :as m} aa] >> (if (= a 50) (assoc m :c 3) m))) >> >> :) >> Jim >> >> >> >> On 30/07/13 18:53, cej38 wrote: >> >> Suppose I have a vector of maps (this could also be a vector of records) >> >> (def aa [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 0} {:a 100 :b 0 :c 0}]) >> >> and I want to go in an change the value of associated with :c when :a >> has a value of 50, so that the final col of maps looks like >> >> [{:a 0 :b 0 :c 0} {:a 50 :b 0 :c 3} {:a 100 :b 0 :c 0}]. >> >> >> >> I came up with a function that does this: >> >> (defn find-assoc-in >> [k v mv] >> (let [f #(if (= ((first k) %) (first v)) >> (assoc % (second k) (second v)) >> %)] >> (map f mv))) >> >> >> (find-assoc-in [:a :c] [50 3] aa) >> >> >> The problem with this function is that it potentially does a lot >> of unnecessary copying of elements that aren't changed, and thus has a lot >> of garbage collection. I would like help coming up with something that >> does much less garbage collection. >> >> >> Will someone please point out a better way of doing this? >> >> -- >> -- >> 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/groups/opt_out. >> >> >> >> >> -- >> -- >> 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/groups/opt_out. >> >> >> > > -- -- 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/groups/opt_out.