Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Created October 19, 2021 18:17
Show Gist options
  • Save mikeananev/f59705b4044948ff02b44be7464c8ebe to your computer and use it in GitHub Desktop.
Save mikeananev/f59705b4044948ff02b44be7464c8ebe to your computer and use it in GitHub Desktop.
Simple queue
(defn new-queue []
(atom clojure.lang.PersistentQueue/EMPTY))
(defn put [queue value]
(when (nil? value)
(throw (ex-info "nil is not allowed" {:value value})))
(swap! queue conj value)
(vec (seq @queue)))
(defn take [queue]
(let [value (peek @queue)]
(swap! queue pop)
value))
(def q (new-queue))
(put q 1)
(put q 2)
(put q 3)
(put q 4)
(put q nil)
(take q)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment