Common macro infix macro to parse a person's arithmetic writing format and execute
(Defmacro infix "Use this macro if you pine for the notation of your childhood" [infixed] (list (second INFI Xed) (first infixed) (last infixed)))
So use:
(infix (1 + 1)); = 2
Check the returned list with Macroexpand
In the above example, the process of removing the last symbol, and then evaluating the remaining list, is actually passed.
(+ 1 2), 3 (+ 1 2)
To know the list before the final evaluation result comes out, you can use Macroexpand. Note to use ' in conjunction ' means that no value is required.
User=> (Macroexpand ' (Ignore-last-operand (+ 1 2 10))) (+ 1 2)
This allows you to display the list returned by the macro, rather than evaluating the value.
Macros change right-to-left reading
(Defn read-resource "read a resource into a string" [path] (read-string (Slurp (clojure.java.io/resource Path))))
This function is evaluated first from the rightmost (because it is also the innermost) list, and Clojure (slurp ...), Path---(read-string ...), then evaluates to the outer layer. The macro can be used to put the first evaluation on the top, written like this.
(Defn read-resource [Path] (with path clojure.java.io/resource slurp read-string))
Everyone has a different preference, and some people still use it in order to reduce the number of brackets and their reading habits from left to right or from top to bottom.
Binding macros
The binding macro is used to create thread-specific variables that are commonly used in multiple threads.
User=> (def ^:d ynamic x 1) user=> (def ^:d ynamic y 1) user=> (+ x Y) 2 user=> (binding [x 2 y 3] (+ x y)) 5 US Er=> (+ x y) 2
When macro
You can accept a bunch of parameters, and if the condition is true, the parameters are evaluated sequentially, returning the result of the last evaluation. When the implementation is used (if and (do).
(Macroexpand ' (When boolean-expression expression-1 expression-2 expression-3); Boolean-expression (Do expression-1 expression-2 expression-3))
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Clojure Language 14 Common macros