Queue/Usage: Difference between revisions

Content added Content deleted
(clojure impl)
Line 174: Line 174:


<lang clojure>
<lang clojure>
(defn make-fifo []
(defn make-queue []
(atom []))
(atom []))


(defn push-fifo [f x]
(defn push-queue [q x]
(swap! f conj x))
(swap! q conj x))


(defn pop-fifo [f]
(defn pop-queue [q]
(if (pos? (count @f))
(if (pos? (count @q))
(let [x (first @f)]
(let [x (first @q)]
(swap! f subvec 1)
(swap! q subvec 1)
x)))
x)))
</lang>
</lang>
Line 189: Line 189:


<lang clojure>
<lang clojure>
(defn make-fifo []
(defn make-queue []
(ref []))
(ref []))


(defn push-fifo [f x]
(defn push-queue [q x]
(alter f conj x))
(alter q conj x))


(defn pop-fifo [f]
(defn pop-queue [q]
(if (pos? (count @f))
(if (pos? (count @q))
(let [x (first @f)]
(let [x (first @q)]
(alter f subvec 1)
(alter q subvec 1)
x)))
x)))
</lang>
</lang>
The <tt>push-fifo</tt> and <tt>pop-fifo</tt> functions would need to be called from within a <tt>dosync</tt> transaction.
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}}==
=={{header|Common Lisp}}==