Queue/Usage: Difference between revisions

Content added Content deleted
(add JavaScript)
(clojure impl)
Line 169: Line 169:
}
}
}</lang>
}</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-fifo []
(atom []))

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

(defn pop-fifo [f]
(if (pos? (count @f))
(let [x (first @f)]
(swap! f subvec 1)
x)))
</lang>
Alternately, one could use a <tt>ref</tt> if coordination between multiple threads was required:

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

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

(defn pop-fifo [f]
(if (pos? (count @f))
(let [x (first @f)]
(alter f subvec 1)
x)))
</lang>
The <tt>push-fifo</tt> and <tt>pop-fifo</tt> functions would need to be called from within a <tt>dosync</tt> transaction.



=={{header|Common Lisp}}==
=={{header|Common Lisp}}==