在 ClojureScript 中更新 Atom 的键
Updating Key of Atom in ClojureScript
这周刚开始使用 Clojure,我正在用头撞墙。虽然我明白 Clojure 中没有什么是可变的,但我并不经常不明白如何使用先前分配的数据更新原子键的值。
我遇到的 2 个简化示例是...
(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))
如有任何帮助,我们将不胜感激!
你已经有了值,所以你可以使用偏函数。
(swap! test-db update :points (partial + 5))
(swap! another-test-db update :nums (partial apply str))
或者更简单地说:
(swap! test-db update :points + 5)
(swap! another-test-db update :nums str)
这周刚开始使用 Clojure,我正在用头撞墙。虽然我明白 Clojure 中没有什么是可变的,但我并不经常不明白如何使用先前分配的数据更新原子键的值。
我遇到的 2 个简化示例是...
(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))
如有任何帮助,我们将不胜感激!
你已经有了值,所以你可以使用偏函数。
(swap! test-db update :points (partial + 5))
(swap! another-test-db update :nums (partial apply str))
或者更简单地说:
(swap! test-db update :points + 5)
(swap! another-test-db update :nums str)