Created
October 19, 2021 18:27
-
-
Save mikeananev/bdd0b95533c1124a603b44ed70ac894c to your computer and use it in GitHub Desktop.
Simple stack implementation in Clojure
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-stack | |
[] | |
(atom [])) | |
(defn pushv [*stack value] | |
(when (nil? value) | |
(throw (ex-info "nil is not allowed" {:value value}))) | |
(swap! *stack conj value) | |
@*stack) | |
(defn popv [*stack] | |
(let [v (peek @*stack)] | |
(when-not (nil? v) | |
(swap! *stack pop)) | |
v)) | |
(def s (new-stack)) | |
(pushv s 1) | |
(pushv s 2) | |
(pushv s 3) | |
(popv s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment