Clojure Language 14 Common macros

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.