This is my current solution. Again, it works fine, but seems a little
verbose. I have a function called update-item, which has to call
update-item-in-category with the same parameters plus the category I'm
currently mapping through.
On Jan 26, 2015 8:11 AM, "Lucas Bradstreet"
wrote:
> Given these
Given these points I would probably just do:
(defn update-val-with-id [values m]
(map (fn [v]
(if (= (:id v) id)
(merge v m)
v))
values))
I'm not that happy with it either, so other suggestions are welcome.
On 27 Janua
They're not keyed by id because order in both the category and its items is
important. I could maintain the order explicitly, but that just makes other
problems like reordering more difficult.
As a couple people have suggested I could access the page using vector
indexing, but then I'd need to fig
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
(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,
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 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
Are you sure you want to return:
[ [ { :id 1 :text "foo" } { :id 2 :text "bar" :ack 5 } ] [ { :id 3
:age 12 :somethingElse 29 } ] ]
(notice the two nested vectors), or rather
[ [ { :id 1 :text "foo" } { :id 2 :text "bar" :ack 5 } { :id 3 :age 12
:somethingElse 29 } ] ]
Lucas
On 26 January 2015 at
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,