If you need simple FIFO queue you can use clojure.lang.PersistentQueue/EMPTY function with some wrappers on it.
;; ------------------------------------------------------------------
(defn new-queue []
(atom clojure.lang.PersistentQueue/EMPTY))
(defn push-queue [queue value]
(swap! queue conj value))
(defn pop-queue [queue]
(let [value (peek @queue)]
(swap! queue pop)
value))
;; ------------------------------------------------------------------
Now let's test it
(def a (new-queue))
;; => nil
(push-queue a 1)
;; => #object...
(push-queue a 2)
;; => #object...
(push-queue a 3)
;; => #object...
(push-queue a "hello")
;; => #object...
Let's check a content of queue a:
(seq @a)
;; => (1 2 3 "hello")
;; ------------------------------------------------------------------
(defn new-queue []
(atom clojure.lang.PersistentQueue/EMPTY))
(defn push-queue [queue value]
(swap! queue conj value))
(defn pop-queue [queue]
(let [value (peek @queue)]
(swap! queue pop)
value))
;; ------------------------------------------------------------------
Now let's test it
(def a (new-queue))
;; => nil
(push-queue a 1)
;; => #object...
(push-queue a 2)
;; => #object...
(push-queue a 3)
;; => #object...
(push-queue a "hello")
;; => #object...
Let's check a content of queue a:
(seq @a)
;; => (1 2 3 "hello")
Now time to take first item:
(pop-queue a)
;; => 1
(seq @a)
;; => (2 3 "hello")
Комментариев нет:
Отправить комментарий