Skip to content

Instantly share code, notes, and snippets.

@brandonbloom
Last active December 13, 2017 21:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonbloom/a46eb4fc98af254160506c70be81f912 to your computer and use it in GitHub Desktop.
Save brandonbloom/a46eb4fc98af254160506c70be81f912 to your computer and use it in GitHub Desktop.
;; manual
(def fuel (atom 10))
(loop []
(when (zero? (swap! fuel dec))
(fail "out of fuel!"))
(recur))
;; auto
(loop []
(check-fuel! 10)
(recur))
(def fuel (atom {}))
(defmacro check-fuel! [n]
(assert (int? n))
(let [tank (assoc (select-keys (meta &form) [:line :column])
:file *file*)]
(assert (and *file* (= (count tank) 3)))
(swap! fuel assoc tank n)
`(do
(swap! fuel update '~tank dec)
(when (zero? (@fuel '~tank))
(swap! fuel assoc '~tank ~n)
(fail "out of fuel!")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment