Clojure更新Atom PersistentQueue

时间:2017-11-22 08:01:21

标签: clojure

我正在努力维持一个订单队列和#34;做饭#34;他们使用FIFO过程。使用原子我能够更新队列,但最后两行不起作用。我需要的是删除第一个元素并用剩余的列表更新原子(队列)。

谢谢, R上。

(ns example.atomqueue
  (:use clojure.pprint))

(def orders-queue (atom clojure.lang.PersistentQueue/EMPTY))

(defrecord Order [fplate splate])

(defn add-order [order]
  (swap! orders-queue conj order))

(add-order (->Order "Soup" "Fish&Chips"))
(pprint orders-queue)

(add-order (->Order "Salad" "Hamburger"))
(pprint orders-queue)

(add-order (->Order "Rice" "Steak"))
(pprint orders-queue)

;;NOT SURE IF CORRECT
(defn cook-order [orders]
  (swap! orders-queue (pop orders)))

;;EXCEPTION
(cook-order orders-queue)

1 个答案:

答案 0 :(得分:0)

感谢leetwinski先生,这是正确的解决方案:

(ns chaptertwo.atom
  (:use clojure.pprint))

(def orders-queue (atom clojure.lang.PersistentQueue/EMPTY))

(defrecord Order [fplate splate])

(defn add-order [order]
  (swap! orders-queue conj order))

(defn cook-order [] (swap! orders-queue pop))

(add-order (->Order "Soup" "Fish&Chips"))
(pprint orders-queue)

(add-order (->Order "Salad" "Hamburger"))
(pprint orders-queue)

(add-order (->Order "Rice" "Steak"))
(pprint orders-queue)

(cook-order )
(pprint orders-queue)