Tav*_*avo 1 swap atomic clojure
我有以下功能:
(defn add-recommendations-to-cache [{:keys [trackingId rec-service recs]} cache]
(assoc-in cache [trackingId rec-service] recs))
Run Code Online (Sandbox Code Playgroud)
我将原子定义为:
(def cache (atom {}))
Run Code Online (Sandbox Code Playgroud)
如果我可以改变传递给函数的参数的顺序,我会使用:
(swap! cache add-recommendations-to-cache msg)
Run Code Online (Sandbox Code Playgroud)
既然我不能,我怎么能swap使用原子,函数和包含第一个参数所需要的消息?我尝试了几种可能的组合(见下文),但似乎都没有.
我试过了:
(swap! cache add-recommendations-to-cache msg cache)
Run Code Online (Sandbox Code Playgroud)
和
(swap! cache (add-recommendations-to-cache msg))
Run Code Online (Sandbox Code Playgroud)
和其他几个没用.
您可以传递自己的函数,以您想要的顺序应用参数:
(swap! cache
(fn [current msg] (add-recommendations-to-cache msg current))
msg)
Run Code Online (Sandbox Code Playgroud)
要么
(swap! cache #(add-recommendations-to-cache %2 %1) msg)
Run Code Online (Sandbox Code Playgroud)
或者关闭msg:
(swap! cache #(add-recommendataions-to-cache msg %))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |