The core role of micro
Because the micro receive parameters can be very complex, and do not evaluate, so you can receive list, in the internal conversion, after processing execution, you can also receive a completely non-Lisp syntax, internally converted to Clojure can execute the list.
This allows you to create your own DSL and implement the Clojure macro as a DSL parser.
The difference between micro and function
It's a feature that allows you to handle it like a function before Clojure evaluate your list, but not evaluate.
If you want to pass an expression as a parameter to the function and ask that the parameter not be evaluated, this is not done. For example, the following function definition will error:
User=> (def ignore-last-operand2 [Function-call] (butlast function-call)) #_ + =
The intention is that Ignore-last-operand2 receives a list parameter, but does not evaluate it, evaluates it while the body is running, and ignores the last symbol of the list.
This situation requires the use of macros.
User=> (Defmacro ignore-last-operand [Function-call] (butlast function-call)) #_ = = #_ = # ' User/ignore-last-operanduser=> (Ignore-last-operand (+ 1 2 10)) 3
and C's macro is a bit like, but C's macro is in the pre-compilation phase to do text replacement, or weak point. Unlike Lisp, you can use macros at run time. 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.
Common macros
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
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Clojure Language 13: macros