Hi, Ivan.
(defn set-item-name [data id newname]
(mapv #(if (not= (:id %) id) ; <- NB mapv keeps it a vector
% ; no-op
(assoc % :name newname))
data))
(dosync (commute items set-item-name 1 "foo"))
This really only makes sense if :id is *not* actually a primary key in your
data,
If, however, :id *is* a primary key, and most of the operations on the data
are only updating one element, you will want to follow Gary's advice and
change your data to
{1 {:id 1 :name ...}
2 {:id 2 :name ...}
...}
I would also like to validate your feeling that this shouldn't be a
one-liner; you can generalize 'set-item-name', and then if you do want to
change the data structure later, you only have to change the general
function.
--Leif
On Tuesday, April 8, 2014 7:10:13 PM UTC-4, Ivan Schuetz wrote:
>
> As I said I already looked in the docs, and know these basic examples, but
> I don't know how to do:
>
> *"How can I say in a vector of records e.g. "set name of element to "foo"
> where id is equal 1"?"*
>
>
> The remove by Id works, I posted it only to show something which might be
> similar to the update I'm looking for.
>
> I also wrote filter:
>
> (nth (filtered (filter #(= (:id %) id) @dataprovider/products)) 0)
>
> This gives me the element I need to update, but I still don't know how I
> update this element in the vector.
>
>
>
> Am Mittwoch, 9. April 2014 00:27:41 UTC+2 schrieb Gary Trakhman:
>>
>> Maybe this will help:
>>
>> > (update-in [[] 2 3 4] [0] (constantly 1))
>> [1 2 3 4]
>>
>> > (update-in [[] 2 3 4] [2] (constantly 1))
>> [ [ ] 2 1 4]
>>
>> > (update-in [[] 2 3 4] [1] (constantly 1))
>> [ [ ] 1 3 4]
>>
>> > (update-in [[] 2 3 4] [0 :a] (constantly :b))
>> java.lang.IllegalArgumentException: Key must be integer
>>
>> > (update-in [[] 2 3 4] [0 0 :a] (constantly :b))
>> [ [{:a :b}] 2 3 4]
>>
>>
>> On Tue, Apr 8, 2014 at 6:22 PM, Ivan Schuetz <[email protected]> wrote:
>>
>>> I would use merge to update the record with the map... but I don't know
>>> how to get it from filter operation. Maybe I should not solve this with
>>> 1-liner.
>>>
>>> Am Mittwoch, 9. April 2014 00:14:09 UTC+2 schrieb Ivan Schuetz:
>>>
>>>> Ahh dataprovider/products should be "items". Forgot to "simplify".
>>>>
>>>>
>>>> Am Mittwoch, 9. April 2014 00:12:48 UTC+2 schrieb Ivan Schuetz:
>>>>>
>>>>> Hi,
>>>>>
>>>>> sorry I don't get it. I just started learning Clojure.
>>>>>
>>>>> I did this to remove element with id 1
>>>>>
>>>>> (commute items #(remove (fn [x](= (:id x) id)) %))
>>>>>
>>>>> From your statement I understand update-in would work for the update,
>>>>> but I don't know the syntax. Something like
>>>>>
>>>>> (commute dataprovider/products #(update-in % {:id id} (->Item
>>>>> ???) ))
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Am Mittwoch, 9. April 2014 00:01:00 UTC+2 schrieb Gary Trakhman:
>>>>>>
>>>>>>
>>>>>>> But 1. Can't find examples with records, 2. Not sure if I can use it
>>>>>>> to update a different field than the one I'm using to do the query. In
>>>>>>> the
>>>>>>> examples fields seem to be the same.
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>> Leave off the last path segment and return the full updated record,
>>>>>> not just the new field's value.
>>>>>>
>>>>> --
>>> 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.
>>>
>>
>>
--
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.