Created
October 19, 2021 18:17
-
-
Save mikeananev/f59705b4044948ff02b44be7464c8ebe to your computer and use it in GitHub Desktop.
Simple queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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