Skip to content

Instantly share code, notes, and snippets.

@mikeananev
Created October 19, 2021 18:27
Show Gist options
  • Save mikeananev/bdd0b95533c1124a603b44ed70ac894c to your computer and use it in GitHub Desktop.
Save mikeananev/bdd0b95533c1124a603b44ed70ac894c to your computer and use it in GitHub Desktop.
Simple stack implementation in Clojure
(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