Last active
December 13, 2017 21:18
-
-
Save brandonbloom/a46eb4fc98af254160506c70be81f912 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; manual | |
(def fuel (atom 10)) | |
(loop [] | |
(when (zero? (swap! fuel dec)) | |
(fail "out of fuel!")) | |
(recur)) | |
;; auto | |
(loop [] | |
(check-fuel! 10) | |
(recur)) |
This file contains hidden or 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
(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