public
Created

  • Download Gist
macros.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
(ns playingwithmacros.core
(:require [clojure.java.browse :refer [browse-url]]
[clojure.pprint :as pp]
[clojure.core.strint :as strint]))
 
(defmacro my-simple-macro
"A docstring"
[first-arg second-arg & varargs]
"A return value of some kind")
 
(my-simple-macro a 2)
 
 
 
;; When does it actually run?
 
(defn test1 []
(my-simple-macro 1 2))
 
;; Add a println side-effect to the macro
;; Re-eval the defn
 
 
 
defmacro
 
 
;;; Explanation by example
 
;;; Debug macro, from http://www.learningclojure.com/2010/09/clojure-macro-tutorial-part-i-getting.html
 
(defmacro dbg
[x]
`(let [x# ~x]
(println '~x "=" x#)
x#))
 
;; Syntax-quote
 
`(1 2 3)
 
'(a b c)
 
`(a b c)
 
;; Namespace-qualified symbols
 
`(+ a b)
 
;; Unquote
 
(let [b 1]
`(a ~b))
 
(read-string "`(a ~b)")
 
;; Splice
 
(let [b ["THIS" "IS" "A" "VECTOR"]]
`(a ~b))
 
(let [b ["THIS" "IS" "A" "VECTOR"]]
`(a ~@b))
 
;; Gensyms
 
(gensym)
 
(defmacro why-gensyms?
[]
`(let [x 1]
x))
 
(why-gensyms?)
 
`(let [x 1]
x)
 
(defmacro why-gensyms?
[]
`(let [x# 1]
x#))
 
(why-gensyms?)
 
`(let [x# 1]
x#)
 
 
;;; Back to the source
;; What are &form and &env?
 
(defmacro env-and-form
[& body]
(dbg &env)
(dbg &form)
nil)
 
;; What's &form?
(env-and-form 23 4234 23)
 
;; What's &env?
(let [a 1
b 2]
(env-and-form))
 
 
;; Metadata on forms
 
(defmacro meta-env-and-form
[& body]
(dbg (meta &env))
(dbg (meta &form)))
 
 
(let [a 1
b 2]
(meta-env-and-form))
 
 
(defn pprint-str
[x]
(with-out-str (pp/pprint x)))
 
(defmacro super-dbg
[x]
`(let [x# ~x]
(printf "dbg %s:%s> %s is %s\n"
~*ns*
~(:line (meta &form))
~(pr-str x)
(pprint-str x#))
(flush)
x#))
 
(let [a 5]
(super-dbg a))
 
;;; Tools
 
(macroexpand '(fn [a]))
 
(macroexpand-1 '(fn [a]))
 
;;; Q: In what order are macros expanded?
 
(defmacro super-fn
[& body]
`(fn ~@body))
 
(macroexpand '(super-fn [a]))
 
(macroexpand-1 '(super-fn [a]))
 
;;; A: outer-first
 
(clojure.walk/macroexpand-all
'(let [a (fn [] blah)]))
 
;;; Q: When does the compiler perform macro-expansion?
 
;; macroexpand-1 gives a hint
 
;; https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L6605
 
 
 
 
 
;;; More useful macros!
 
;; https://github.com/flatland/useful/blob/develop/src/flatland/useful/map.clj#L5
 
(let [transforms {:keys keyword
:strs str
:syms identity}]
(defmacro keyed
"Create a map in which, for each symbol S in vars, (keyword S) is a
key mapping to the value of S in the current scope. If passed an optional
:strs or :syms first argument, use strings or symbols as the keys instead."
([vars] `(keyed :keys ~vars))
([key-type vars]
(let [transform (comp (partial list `quote)
(transforms key-type))]
(into {} (map (juxt transform identity) vars))))))
 
(let [a 1
b 2
c 3]
(keyed [a b c]))
 
 
;;; just for fun
 
(defmacro dbg-macro
[x]
(let [expansion (clojure.walk/macroexpand-all x)]
`(do (printf "dbg-macro> %s expands to %s\n"
~(pr-str x)
'~expansion)
(flush)
~x)))
 
;; https://github.com/clojure/core.incubator/blob/master/src/main/clojure/clojure/core/strint.clj#L49
 
(dbg-macro (strint/<< "Chas is great times ~(+ 1 2)!"))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.