Now that I think about it, I wonder why your categories aren't keyed by id, 
like this:

(def categories [ {1 {:text "foo"} 2 {:text "bar" :ack 5}} {3 {:age 7}}])

Then the update-in can take the category id, instead of having to know its 
index within a vector:

(update-in categories [1 3]  merge { :age 12 :somethingElse 29 })
[{1 {:text "foo"}, 2 {:text "bar", :ack 5}} {3 {:age 12, :somethingElse 
29}}]


On Monday, January 26, 2015 at 9:42:56 AM UTC-6, Michael Willis wrote:
>
> (def categories [ [ { :id 1 :text "foo" } { :id 2 :text "bar" :ack 5 } ] [ 
> { :id 3 :age 7 } ] ])
> #'user/categories
> (update-in categories [1 0] merge { :age 12 :somethingElse 29 })
> [[{:text "foo", :id 1} {:text "bar", :ack 5, :id 2}] [{:age 12, 
> :somethingElse 29, :id 3}]]
>
>
> On Monday, January 26, 2015 at 8:54:58 AM UTC-6, Erik Price wrote:
>>
>> Many functions that affect keyed collections will work on vectors if you 
>> supply the numeric index as a key.
>>
>> e
>>
>> On Monday, January 26, 2015, Josh Stratton <stratto...@gmail.com> wrote:
>>
>>> I'm new to clojure and FP in general.  One thing that has always been a 
>>> little confusing for me is working with immutable trees.  I have a vector 
>>> of categories, each category containing a vector of items--each one a 
>>> hashmap.  In that hashmap I have a bunch of attributes including an 
>>> item-id.  Now, assuming I have an item id, what's the easiest way to update 
>>> the appropriate hashmap?  I can't use an assoc, I believe, because my data 
>>> is in a vector--not keyed by the id.  
>>>
>>> What I have been doing is writing a function that maps the categories to 
>>> new categories and then write another function that is called on every item 
>>> and updates it iff the item id matches.  This works, but it seems really 
>>> clunky and I'm assuming there's a simpler way to do it.  
>>>
>>> ; categories is a vector of item vectors, where the item is a hash
>>> (def categories [ [ { :id 1 :text "foo" } { :id 2 :text "bar" :ack 5 } ] 
>>> [ { :id 3 :age 7 } ] ])
>>>
>>> Is there a more elegant workflow for updating categories?  Let's say I 
>>> want to update item of id 3 with { :age 12 :somethingElse 29 }, what's the 
>>> easiest way to do this?  
>>>
>>> ; so the return would be
>>> [ [ { :id 1 :text "foo" } { :id 2 :text "bar" :ack 5 } ] [ { :id 3 :age 
>>> 12 :somethingElse 29 } ] ]
>>>
>>> -- 
>>> 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.

Reply via email to