Discussion:
Key must be integer error
Renata Soares
2018-04-29 14:06:33 UTC
Permalink
Hi,

How can I update the atom accounts in the field of operations?

(def accounts (atom [{:id "7512a15b-0770-4a9b-a74b-389374b46461", :balance
0.0, :operations nil, :blocked false}
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994", :balance 0.0, :operations
nil, :blocked false}
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9", :balance 0.0, :operations
nil, :blocked false}
{:id "2dc20615-f1f7-4637-9602-ae9494689166", :balance 0.0, :operations
nil, :blocked false}
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd", :balance 0.0, :operations
nil, :blocked false}]))

I tried this:

(swap! accounts (fn [x] (update-in x [:operations] #(if (= (:id %)
account-id) new-value))))

and returns "key must be integer" because of [:operations]

Thanks.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@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+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Axel Katerbau
2018-04-29 14:12:22 UTC
Permalink
Hi Renata,
Post by Renata Soares
How can I update the atom accounts in the field of operations?
(def accounts (atom [{:id "7512a15b-0770-4a9b-a74b-389374b46461", :balance 0.0, :operations nil, :blocked false}
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994", :balance 0.0, :operations nil, :blocked false}
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9", :balance 0.0, :operations nil, :blocked false}
{:id "2dc20615-f1f7-4637-9602-ae9494689166", :balance 0.0, :operations nil, :blocked false}
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd", :balance 0.0, :operations nil, :blocked false}]))
(swap! accounts (fn [x] (update-in x [:operations] #(if (= (:id %) account-id) new-value))))
and returns "key must be integer" because of [:operations]
the atom hold and array of accounts. In swap! you are trying to access one (!) value in a key-path [:operations] but access to elements of an array is not valid via a key-path but by using indexes.

What you are seemingly trying to do is to map over the contents of the array in the atom which is not what you coded here.

- Axel
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@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+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tim Visher
2018-04-29 17:03:09 UTC
Permalink
Hi Renata,

```
user=> (clojure.pprint/pprint (swap! accounts #(map (fn [a] (if (= (:id a)
"3136860d-ab7f-4814-8f3b-2d18048db9b9") (assoc a :operations :foo) a)) %)))
({:id "7512a15b-0770-4a9b-a74b-389374b46461",
:balance 0.0,
:operations nil,
:blocked false}
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994",
:balance 0.0,
:operations nil,
:blocked false}
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9",
:balance 0.0,
:operations :foo,
:blocked false}
{:id "2dc20615-f1f7-4637-9602-ae9494689166",
:balance 0.0,
:operations nil,
:blocked false}
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd",
:balance 0.0,
:operations nil,
:blocked false})
```

Your swapping function will have to be map since you're trying to update
some matching component of a vector. Then you can test if the current item
is your target `(if (= (:id a) 
)` at which point you can change it `(assoc
a :operations 
)`. Otherwise, you need to return it so you don't lose any
data.

Note that in this case it might make some sense to transform your data to a
map since that allows for simple key lookups and would allow for the use of
`assoc-in`:

```
user=> (clojure.pprint/pprint @accounts)
{"7512a15b-0770-4a9b-a74b-389374b46461"
{:id "7512a15b-0770-4a9b-a74b-389374b46461",
:balance 0.0,
:operations nil,
:blocked false},
"7446cfe6-882c-4f25-bad1-5ed8c9aea994"
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994",
:balance 0.0,
:operations nil,
:blocked false},
"3136860d-ab7f-4814-8f3b-2d18048db9b9"
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9",
:balance 0.0,
:operations nil,
:blocked false},
"2dc20615-f1f7-4637-9602-ae9494689166"
{:id "2dc20615-f1f7-4637-9602-ae9494689166",
:balance 0.0,
:operations nil,
:blocked false},
"c6dccf4a-535c-4eba-8cfc-0554408de8bd"
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd",
:balance 0.0,
:operations nil,
:blocked false}}
nil
user=> (clojure.pprint/pprint (swap! accounts #(assoc-in %
["3136860d-ab7f-4814-8f3b-2d18048db9b9" :operations] :bar)))
{"7512a15b-0770-4a9b-a74b-389374b46461"
{:id "7512a15b-0770-4a9b-a74b-389374b46461",
:balance 0.0,
:operations nil,
:blocked false},
"7446cfe6-882c-4f25-bad1-5ed8c9aea994"
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994",
:balance 0.0,
:operations nil,
:blocked false},
"3136860d-ab7f-4814-8f3b-2d18048db9b9"
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9",
:balance 0.0,
:operations :bar,
:blocked false},
"2dc20615-f1f7-4637-9602-ae9494689166"
{:id "2dc20615-f1f7-4637-9602-ae9494689166",
:balance 0.0,
:operations nil,
:blocked false},
"c6dccf4a-535c-4eba-8cfc-0554408de8bd"
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd",
:balance 0.0,
:operations nil,
:blocked false}}
```
Post by Axel Katerbau
Hi Renata,
Post by Renata Soares
How can I update the atom accounts in the field of operations?
(def accounts (atom [{:id "7512a15b-0770-4a9b-a74b-389374b46461",
:balance 0.0, :operations nil, :blocked false}
Post by Renata Soares
{:id "7446cfe6-882c-4f25-bad1-5ed8c9aea994", :balance 0.0,
:operations nil, :blocked false}
Post by Renata Soares
{:id "3136860d-ab7f-4814-8f3b-2d18048db9b9", :balance 0.0,
:operations nil, :blocked false}
Post by Renata Soares
{:id "2dc20615-f1f7-4637-9602-ae9494689166", :balance 0.0,
:operations nil, :blocked false}
Post by Renata Soares
{:id "c6dccf4a-535c-4eba-8cfc-0554408de8bd", :balance 0.0,
:operations nil, :blocked false}]))
Post by Renata Soares
(swap! accounts (fn [x] (update-in x [:operations] #(if (= (:id %)
account-id) new-value))))
Post by Renata Soares
and returns "key must be integer" because of [:operations]
the atom hold and array of accounts. In swap! you are trying to access one
(!) value in a key-path [:operations] but access to elements of an array is
not valid via a key-path but by using indexes.
What you are seemingly trying to do is to map over the contents of the
array in the atom which is not what you coded here.
- Axel
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
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
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 ***@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+***@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+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...