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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.