【问题标题】:Updating Key of Atom in ClojureScript在 ClojureScript 中更新 Atom 的键
【发布时间】:2016-03-24 21:47:29
【问题描述】:

这周刚开始使用 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))

任何帮助将不胜感激!

【问题讨论】:

    标签: clojure clojurescript


    【解决方案1】:

    你已经有了值,所以你可以使用偏函数。

    (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)
    

    【讨论】:

    • 不客气。 SO上最好的“谢谢”是接受答案。 ;)
    • 哈哈对不起,我不知道我能做到这一点——以为那只是外人的!我找到你了。
    • 在这两种情况下,单词 partial 和它的包围 ( ) 都可以删除,您可能会在更短的时间内得到相同的结果
    • 对,你是@ArthurUlfeldt 和@LeoGrapenthin!编辑了我的答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多