Clojure macro

At first, I want to add before/after to FacthCheck(Julia test framework).

But I don't understand macro well and there is soo little material about Julia macro,

and Clojure is similar to Julia, so I try to learn Clojure macro.

The macro

pass some expressions and return a single expression out

How it works

Clojure-compile-time-and-run-time

Clojure is a compiled language.

The compiler reads source files or strings, produces data structures (aka the AST)

and performs macroexpansion.

Macros are evaluated at compile time and produce modified data structures

that are compiled to the JVM bytecode.

That bytecode is executed at run time.

'macro transform an expression into something else, before runtime'

How to write macro

When a macro is called, it will generate code.

1. write example about how the macro is used

2. write the wishful macro result done

3. decompose the problem

same procedure as you write other kind of code,

but those should be under concern:

1. the args passed are "expression"

2. the output is one expression.

3. it is cool to compose the expression.

4. take care "Anaphora" and "Hygiene"

How to use macro

' quote

` syntax-quoting  # foo# gen symbol auto

~ unquote  # causes a quoted form to be evaluated

~@ unquote-splice

`'~v evalue v, and quote it, then solve it in current environment

Quote vs Syntax-quoting

http://blog.8thlight.com/colin-jones/2012/05/22/quoting-without-confusion.html

1. with namespace

2. will eval the from in an unquote

3. can generate symbol by "foo#"

tricks:

~'c without the namespace symbol c

~`c with the namespace symbol c

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容