Is there an easier way to write this code in Clojure:
(def queue (atom {:top nil :queue PersistentQueue/EMPTY})) (swap! queue #(hash-map :top nil :queue (conj (:queue %) "foo"))) (let [{:keys [top]} (swap! queue #(hash-map :top (peek (:queue %)) :queue (pop (:queue %))))] (println top))
Alternative recording method:
(def queue (atom PersistentQueue/EMPTY)) (swap! queue conj "foo") (let [top (atom nil)] (swap! queue (fn [queue] (reset! top (peek queue)) (pop queue))) (println @top))
It seems even worse.
In any case, I have code that uses atoms for mass servicing, and using the previous approach makes the code really confusing, I expect there will be something like:
(swap! queue (fn [queue] (AtomSwapResult. atom-value return-value))
or some kind of similar mechanism in a swap! because it looks like what you would like to do often (even without being limited to the queue, I hit several other use cases when it would be useful to return another value, for example, the old value that was replaced) and it does not break the atom / swap ! semantics.
Is there a way to do this in Clojure?
source share