memoize
不佔結合,這可以通過查看在其中的原子地圖僅由參數鍵控源來確認。實際上,動態重新綁定的功能不是「引用透明」的(即不能用其值替代)。
是否有什麼能夠阻止您將*config-val*
作爲參數傳遞,至少是您想要記憶的函數?
user=> (source memoize)
(defn memoize
"Returns a memoized version of a referentially transparent function. The
memoized version of the function keeps a cache of the mapping from arguments
to results and, when calls with the same arguments are repeated often, has
higher performance at the expense of higher memory use."
{:added "1.0"}
[f]
(let [mem (atom {})]
(fn [& args]
(if-let [e (find @mem args)]
(val e)
(let [ret (apply f args)]
(swap! mem assoc args ret)
ret)))))
不是真的我只是試圖找出方法來重組這個,我已經得到了一些很好的工作代碼,我只是想辦法讓它快很多。 – toofarsideways