->
宏在第二个版本中增加了围绕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