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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子咒林,更是在濱河造成了極大的恐慌坝锰,老刑警劉巖杖剪,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件聋呢,死亡現(xiàn)場離奇詭異诡壁,居然都是意外死亡洪橘,警方通過查閱死者的電腦和手機(jī)跪者,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來熄求,“玉大人渣玲,你說我怎么就攤上這事〉芡恚” “怎么了忘衍?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵逾苫,是天一觀的道長。 經(jīng)常有香客問我枚钓,道長铅搓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任搀捷,我火速辦了婚禮星掰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嫩舟。我一直安慰自己蹋偏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布至壤。 她就那樣靜靜地躺著威始,像睡著了一般。 火紅的嫁衣襯著肌膚如雪像街。 梳的紋絲不亂的頭發(fā)上黎棠,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天,我揣著相機(jī)與錄音镰绎,去河邊找鬼脓斩。 笑死,一個(gè)胖子當(dāng)著我的面吹牛畴栖,可吹牛的內(nèi)容都是我干的随静。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼吗讶,長吁一口氣:“原來是場噩夢啊……” “哼燎猛!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起照皆,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤重绷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后膜毁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昭卓,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年瘟滨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了候醒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,680評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡杂瘸,死狀恐怖倒淫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胧沫,我是刑警寧澤昌简,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布占业,位于F島的核電站,受9級特大地震影響纯赎,放射性物質(zhì)發(fā)生泄漏谦疾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一犬金、第九天 我趴在偏房一處隱蔽的房頂上張望念恍。 院中可真熱鬧,春花似錦晚顷、人聲如沸峰伙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瞳氓。三九已至,卻和暖如春栓袖,著一層夾襖步出監(jiān)牢的瞬間匣摘,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工裹刮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留音榜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓捧弃,卻偏偏與公主長得像赠叼,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子违霞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容