Created
January 16, 2013 17:35
-
-
Save swannodette/4549031 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
(defmacro assumedTrue | |
"will throw if any of the passed expressions evaluate to false or nil" | |
[& _] ;allows 0 or more params, but 0 params will throw and allow you to see the original line number | |
;(pri "` lexical env: `" ~a) | |
`(do | |
(let [myname# '~(first &form) ;aka the name of this macro | |
allPassedForms# '~(rest &form) ;all parameters passed to this macro | |
] | |
(cond (<= (count allPassedForms#) 0) | |
(throw | |
(AssertionError. | |
(str "you passed no parameters to `" myname# "`") | |
) | |
) | |
) | |
(loop [allparams# allPassedForms#] | |
( do | |
(let [ | |
exactform# (first allparams#) | |
evaluatedForm# (eval exactform#) | |
] | |
;(prn "all params:" '~(rest &form)) | |
(prn "exactform#:" exactform#) | |
(prn "evalled:" evaluatedForm# "rest count:" (count allparams#)) | |
;(prn "third:" evaluatedForm#) | |
;true) | |
;(prn "aT:" (quote ~x) f# eva#) | |
(when-not evaluatedForm# | |
(do | |
(throw | |
(AssertionError. | |
(str | |
myname# " failed " | |
exactform# | |
" was " | |
evaluatedForm# | |
) | |
) | |
) | |
) | |
) | |
(cond (<= (count allparams#) 1);aka no more | |
true | |
:else | |
(do | |
(prn "COUNT:" (count allparams#) "rest:" (rest allparams#)) | |
( recur (rest allparams#)) | |
) | |
) | |
) | |
)) | |
) | |
) | |
) ;macro end | |
;TODO: make tests for this macro | |
;(assumedTrue 1 2 3 (> 2 1) (= :a :a) (= 1 2)) | |
;(assumedTrue) | |
(defn somef_ [a] (assumedTrue (= 3 a))) | |
(somef_ 3) | |
(somef_ 2) | |
;CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(runtime\q.clj:61:3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment