Alternate version of swap! also returning swapped out value

后端 未结 5 865
栀梦
栀梦 2021-01-20 03:33

I talked about this a bit on IRC\'s #clojure channel today but would like to go more in detail here. Basically, in order to better understand atoms, swap!

5条回答
  •  时光取名叫无心
    2021-01-20 04:32

    You could rely on a promise to store the current value inside the swap! operation. Then you return the new and old value in a vector, as follows:

    (defn- swap-and-return-old-value!
      [^clojure.lang.IAtom atom f & args]
      (let [old-value-promise (promise)
            new-value (swap! atom
                         (fn [old-value]
                           (deliver old-value-promise old-value)
                           (apply f old-value args)))]
        [new-value @old-value-promise]))
    

提交回复
热议问题