### Tools for building macros
(clean-code code)Recursively unqualifies qualified code in the provided form, fully-qualifying back any unqualified symbol with a namespace.
(clean-code `(a (b c)))
=> '(a (b c))
Recursively unqualifies qualified code in the provided form, fully-qualifying back any unqualified symbol with a namespace. ```clojure (clean-code `(a (b c))) => '(a (b c)) ```
(file-eval code)Evaluate code in a temporary file via load-file in the local
lexical context. Keep the temporary file aside if an error is
raised, deleting it on the next run.
(let [a 1]
(file-eval '(+ 1 a)))
Code evaluated this way will be source-mapped in stacktraces.
Evaluate code in a temporary file via `load-file` in the local lexical context. Keep the temporary file aside if an error is raised, deleting it on the next run. ```clojure (let [a 1] (file-eval '(+ 1 a))) ``` Code evaluated this way will be source-mapped in stacktraces.
(fully-unwrap-form sym code & {:keys [count] :or {count false}})Recursively calls unwrap-form until the form is fully unwrapped.
Recursively calls [[unwrap-form]] until the form is fully unwrapped.
(is-form? sym code)Determines whether code is a form starting with sym.
(is-form? 'a 1) ; => false
(is-form? 'a '[a :z]) ; => false
(is-form? 'a '(a :z)) ; => true
Determines whether `code` is a form starting with `sym`. ```clojure (is-form? 'a 1) ; => false (is-form? 'a '[a :z]) ; => false (is-form? 'a '(a :z)) ; => true ```
(macroexpand-all-eager expr & {:as opts})Like clojure.walk/macroexpand-all but does not expand quoted forms.
(defmacro m [] :abc)
(clojure.walk/macroexpand-all '((m) (quote (m))))
=> (:abc (quote :abc))
(macroexpand-all-eager '((m) (quote (m))))
=> (:abc (quote (m)))
Like clojure.walk/macroexpand-all but does not expand quoted forms. ```clojure (defmacro m [] :abc) (clojure.walk/macroexpand-all '((m) (quote (m)))) => (:abc (quote :abc)) (macroexpand-all-eager '((m) (quote (m)))) => (:abc (quote (m))) ```
(macroexpand-depth n expr)Expands the first n levels of expr with
macroexpand-all-eager.
Expands the first `n` levels of `expr` with [[macroexpand-all-eager]].
(macroexpand-do & args)(defmacro abc [] `(println "xyz"))
(macroexpand-do (abc))
; -- Macro expansion -- ; (clojure.core/println "xyz") ; ; -- Running macro -- ; xyz
or alternatively:
(macroexpand-do MODE expr)
Where MODE has the following meaning:
MODE | expansion |
|---|---|
nil (the default) | macroexpand |
:all | macroexpand-all-eager |
| :n <number> | macroexpand-n |
| :depth <number> | macroexpand-depth |
| :some <fn> | macroexpand-some |
(defmacro abc [] `(println "xyz")) (macroexpand-do (abc)) ; -- Macro expansion -- ; (clojure.core/println "xyz") ; ; -- Running macro -- ; xyz or alternatively: (macroexpand-do MODE expr) Where `MODE` has the following meaning: | `MODE` | expansion | |-------------------------------|----------------------------------| | `nil` (the default) | `macroexpand` | | `:all` | `macroexpand-all-eager` | | :n <number> | `macroexpand-n` | | :depth <number> | `macroexpand-depth` | | :some <fn> | `macroexpand-some` |
(macroexpand-n n form)Iteratively call macroexpand-1 on form n times.
Iteratively call `macroexpand-1` on `form` `n` times.
(macroexpand-some pred expr)Recursively macroexpands seqs in expr whose first element matches
pred. Symbols are passed to filter, their namespace stripped.
Quoted forms are not expanded.
(macroexpand-some #{'let}
'(let [a (let [aa 1] aa)
b '(let [bb 2] bb)]
(dosync [a b])))
=> (let* [a (let* [aa 1] aa) ; expanded
b '(let [bb 2] bb)] ; not expanded since quoted
(dosync nil)) ; not expanded b/c pred does not match
Recursively macroexpands seqs in `expr` whose first element matches
`pred`. Symbols are passed to `filter`, their namespace stripped.
Quoted forms are not expanded.
```clojure
(macroexpand-some #{'let}
'(let [a (let [aa 1] aa)
b '(let [bb 2] bb)]
(dosync [a b])))
=> (let* [a (let* [aa 1] aa) ; expanded
b '(let [bb 2] bb)] ; not expanded since quoted
(dosync nil)) ; not expanded b/c pred does not match
```(unwrap-form sym code)Unwraps code if it is a form starting with sym. Returns code
otherwise.
(unwrap-form 'a '(a :z)) ; a
(->> '(a :z) (unwrap-form 'a) (unwrap-form 'a)) ; a
Unwraps `code` if it is a form starting with `sym`. Returns `code` otherwise. ```clojure (unwrap-form 'a '(a :z)) ; a (->> '(a :z) (unwrap-form 'a) (unwrap-form 'a)) ; a ```
(wrap-form sym code)Wraps code in a form begining with sym unless it is already
the case.
(wrap-form 'a :z) ; => (a :z)
(->> :z (wrap-form 'a) (wrap-form 'a)) ; => (a :z)
Wraps `code` in a form begining with `sym` unless it is already the case. ```clojure (wrap-form 'a :z) ; => (a :z) (->> :z (wrap-form 'a) (wrap-form 'a)) ; => (a :z) ```
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |