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