I would have written the fn like this, if I was following what you've been doing:
(def m {:planet {:country {:state {:city {:borough 4}}}}}) (let [mm (update-in m [:planet :country :state :city :borough ] (fnil inc -1))] (get-in mm [:planet :country :state :city :borough ])) However, when I run into this pattern I usually (let [v ((fnil inc -1) (get-in m [:planet :country :state :city :borough ]))] (assoc-in m [:planet :country :state :city :borough ] v)) The update in does a get, and I do a get-in, so I'm guessing the performance is pretty similar, though I haven't tested it. Cheers, Jay On Thu, Apr 26, 2012 at 12:00 PM, blais <goo...@furius.ca> wrote: > Hi, > I have this use-case for (update-in) which keeps showing up in my code > which I don't have a solution for and I'm wondering if there isn't a > solution. > How do I _efficiently_ obtain the new value created by an invocation of > (update-in), that is, without having to get the modified value by lookups > in the new map? > > Simplified example: > > (def m {:planet {:country {:state {:city {:borough 4}}}}}) > > (let [mm (update-in m > [:planet :country :state :city :borough] > (fn [old] (if (nil? old) 0 (inc old))))] > (get-in mm [:planet :country :state :city :borough])) > > (update-in) returns the new/modified map, but what I want is to obtain the > new count returned by the anonymous function. > Having to call (get-in) right after is inefficient. > > This is obviously a contrived example for this question, but I have a lot > of real use cases for this (where at the top level of my app I have a ref > for a deep structure which changes as a response to network events). > > Is there an idiomatic way to do this without having to resort to > mutability? > (The only solution that comes to my mind is to wrap up update-in and the > modifier function with a special version that updates a local mutable and > return both the new map and the new object.) > Nasty? > > Thank you, > > > > -- > 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 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