That's cool, but I may be reading this from a file like json, applying the change, and writing it back. Changing it to a sorted list would take linear time anyway, right? And I'd just be writing it back out immediately. Still this seems like a very clean solution. I'll have to read up on how update-in syntax with sorted maps. Thanks.
On Wed, Jan 14, 2015 at 10:31 PM, Fluid Dynamics <a2093...@trbvm.com> wrote: > On Thursday, January 15, 2015 at 1:24:14 AM UTC-5, strattonbrazil wrote: >> >> Noob clojure question. I have a list of items and I want to append a >> subitem to one of them. They're currently unkeyed because I care about the >> order and it seemed more intuitive that way. >> >> (def items [{ :id 1 :subitems [] } { :id 2 :subitems [] }]) >> >> So let's say I want a new list where the second one has two a new subitem >> would a map be appropriate, but only operate on the matching id? >> > > Blerk. That's going to be linear time. Why not use sorted-map? > > (def items (into (sorted-map) {1 {:id 1 :subitems []} 2 {:id 2 :subitems > []})) > > (defn add-subitem [items id subitem] > (update-in items [id :subitems] conj subitem)) > > Now you can just pick the item out you want by its id, without a linear > search, but they'll still be sorted by id. > > -- > 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. > -- 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.