【发布时间】:2013-02-26 06:14:00
【问题描述】:
我有两个要比较的序列,我需要将比较结果保存在映射中,第一个序列中的数据用作键,第二个序列中的数据用作 val。下面是可以工作的示例代码
(def myAtom (atom {}))
(map #(if (== %1 %2) (swap! myAtom assoc %1 %2 ))
[1 2 3] [4 5 3])
(prn @myAtom) ; ==> {3 3}
但是,将上面的“相同”的东西放入一个 let 绑定后,它就不再起作用了
(let [ myAtom (atom {})]
(map #(if (== %1 %2) (swap! myAtom assoc %1 %2 ))
[1 2 3] [4 5 3])
(prn @myAtom)) ;;==> {} empty???
所以问题是,在 let 绑定中 myAtom 会发生什么?怎么不见了?
【问题讨论】:
-
如果没有“else”分支,你可以使用
when而不是if。
标签: dictionary clojure swap let