I wanted a Clojure vector in an atom to model a stateful FIFO (push to the end, pop from the beginning). tried
(def stack (atom []))
then push as follows:
(swap! stack #(conj % 1))
(swap! stack #(conj % 2))
expecting
[1 2]
but getting
(2 1)
Not a big deal, it just means that I must reverse (O(n)) the value of the atom (persistent list) to get the items in the order than I pushed them (e.g., a stream of imperative commands to a virtual machine in order). Still, it was a surprise.
Is there a clojure.core FIFO I can pack into an atom? I thought of priority-map, but it seems overkill. The examples for swap! on clojuredocs.org use lists or maps, not quite what I wanted. I found lots of samples by googling "FIFO Clojure," but some are a bit rich, e.g., clojure.core.cache (map-like and not vector-like); amalloy's ring-buffer (external dependency). Looking for something really small and straightforward. I didn't see an answer in the automatic suggestions from StackOverflow.