Is there a simpler 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 way to write it would be :
(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))
That seems even worse.
Anyway I have a code which uses atoms for queuing a lot and using the former approach is making the code really confusing, I would expect there to be something like :
(swap! queue (fn [queue] (AtomSwapResult. atom-value return-value))
or some similar mechanism in the swap! function since it seems like the kind of thing you would want to do often (not even limited to queuing, I've hit several other use cases where it would be useful to return a different value, for eg. the old value that was swapped out) and it doesn't break the atom/swap! semantics.
Is there a way to do this in Clojure ?