简体   繁体   中英

Updating Key of Atom in ClojureScript

just started Clojure this week and am banging my head against a wall on something. While I understand that nothing is mutable in Clojure, I don't often don't understand how to update the value of an atom's key using the data it had previously been assigned.

2 simplified examples I'm struggling with are...

(def test-db (atom
{:name "jessie" :points 4}))

(swap! test-db update :points (:points + 5))


(def another-test-db (atom
{:name "roger" :nums [1 2 3]}))

(swap! another-test-db update :nums (apply str :nums))

Any help would be greatly appreciated!

You already have the value in hand, so you can use partial functions.

(swap! test-db update :points (partial + 5))
(swap! another-test-db update :nums (partial apply str))

Or more simply:

(swap! test-db update :points + 5)
(swap! another-test-db update :nums str)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM