On 24.09.2012 13:04, Mond Ray wrote:
> user=> wish-lists
> [{:name "WL1", :items [{:name "Item 1", :cost 20.0} {:name "Item 2",
> :cost 40.0}]} {:name "WL2", :items [{:name "Wiggle 1", :cost 20.0}
> {:name "Wiggle 2", :cost 40.0} [:name "Item 3" :cost 10.0]]}]

> user=> (assoc-in wish-lists [:name "WL1"] [:name "WL1" :items new-wi])
> IllegalArgumentException Key must be integer
> clojure.lang.APersistentVector.assoc (APersistentVector.java:312)

`assoc-in` navigates nested data structures by keys. Like George said,
vectors' keys are integer indices, so you'd have to use `[0]` as the
path to refer to the first wish list. To navigate the data structure
using list names, change your `wish-lists` structure into a map with the
names as keys.

    (def wish-lists {"WL1" [{:name "Item 1", :cost 20.0}
                            {:name "Item 2", :cost 40.0}]
                     "WL2" [{:name "Wiggle 1", :cost 20.0}
                            {:name "Wiggle 2", :cost 40.0}]})

`assoc-in` is for adding new key-value pairs to a map, so it isn't quite
what you need to append to the wish list vector. There's a more generic
function called `update-in`, which takes a function to update the thing
at the end of the path. `conj` appends items to a vector, so that's what
you should use as the update function.

    (update-in wish-lists ["WL1"] conj {:name "Item 3" :cost 10.0})
    ;= {"WL1" [{:name "Item 1", :cost 20.0}
               {:name "Item 2", :cost 40.0}
               {:name "Item 3", :cost 10.0}]
        "WL2" [{:name "Wiggle 1", :cost 20.0}
               {:name "Wiggle 2", :cost 40.0}]}

Hope this helps,
-- 
Timo

-- 
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

Reply via email to