2016-01-05 22 views
1

最后一个参数,我有以下功能:交换与原子作为被使用的功能

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

和其他几个人就没什么用了。

回答

7

你可以通过你自己的函数,在该命令所适用的参数,你想要的:

(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 %)) 
+0

我觉得一个关闭味精是整体上更好。 –