Skip to content

Instantly share code, notes, and snippets.

@athos
Created August 1, 2012 14:43
Show Gist options
  • Save athos/3227434 to your computer and use it in GitHub Desktop.
Save athos/3227434 to your computer and use it in GitHub Desktop.
(use 'syntactic-closure.core)
(define-syntax aif
(sc-macro-transformer
(fn [[_ test then else] env]
(quasiquote
(let [it ~(make-syntactic-closure env nil test)]
(if it
~(make-syntactic-closure env '[it] then)
~(make-syntactic-closure env nil else)))))))
(define-syntax awhen
(sc-macro-transformer
(fn [[_ test & body] env]
(quasiquote
(aif ~(make-syntactic-closure env nil test)
(do ~@(map #(make-syntactic-closure env '[it] %) body))
nil)))))
(def m {:x "42"})
(aif (:x m)
(Integer/parseInt it)
0)
(awhen (:x m)
(println "found x")
(println it))
(macroexpand
'(awhen (:x m)
(println "found x")
(println it)))
;=>
(let* [it465 (:x user/m)]
(if it465
(do
(clojure.core/println "found x")
(clojure.core/println it465))
nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment