Queue/Usage: Difference between revisions

(→‎{{header|Clojure}}: moved to FIFO)
Line 169:
}
}</lang>
 
=={{header|Clojure}}==
The "pop" function implies mutating the input, but since Clojure data structures are immutable we use a mutable reference to an immutable data structure; in this case an <tt>atom</tt> holding a vector:
 
<lang clojure>
(defn make-queue []
(atom []))
 
(defn push-queue [q x]
(swap! q conj x))
 
(defn pop-queue [q]
(if (pos? (count @q))
(let [x (first @q)]
(swap! q subvec 1)
x)))
</lang>
Alternately, one could use a <tt>ref</tt> if coordination between multiple threads was required:
 
<lang clojure>
(defn make-queue []
(ref []))
 
(defn push-queue [q x]
(alter q conj x))
 
(defn pop-queue [q]
(if (pos? (count @q))
(let [x (first @q)]
(alter q subvec 1)
x)))
</lang>
The <tt>push-queue </tt> and <tt>pop-queue </tt> functions would need to be called from within a <tt>dosync</tt> transaction.
 
=={{header|Common Lisp}}==
Anonymous user