->
宏在第二個版本中增加了圍繞apply-str
的變形,這就是爲什麼宏擴展到最終調用函數的代碼。看看爲->
的源代碼,你可以看到:
(defmacro ->
"Threads the expr through the forms. Inserts x as the
second item in the first form, making a list of it if it is not a
list already. If there are more forms, inserts the first form as the
second item in second form, etc."
([x] x)
([x form] (if (seq? form)
(with-meta `(~(first form) ~x [email protected](next form)) (meta form))
(list form x)))
([x form & more] `(-> (-> ~x ~form) [email protected])))
相關的部分是,當它在處理兩個參數,x
和form
。如果form
是seq,則將x
作爲該列表中的第二個參數插入。否則,宏將它自己放入一個列表中,並將它放入列表form
和x
。這樣您就可以使用裸符號作爲包含一個符號的列表的簡寫。
user> (macroexpand '(-> 123 (foo)))
(foo 123)
user> (macroexpand '(-> 123 foo))
(foo 123)
我想寫的代碼比這更復雜一些。這只是一個簡單的例子。我只想更好地理解' - >'宏如何工作。 :-) – 2010-04-05 11:39:38
啊,陷阱。無論如何,你看到這裏發生了什麼? – 2010-04-05 14:02:59
是的,我願意。感謝您的回答! – 2010-04-09 11:35:53