以下代码的行为与我的预期不符。
; given a function name, its args and body, create 2 versions:
; i.e., (double-it foo []) should create 2 functions: foo and foo*
(defmacro double-it
[fname args & body]
`(defn ~fname ~args ~@body)
`(defn ~(symbol (str fname "*")) ~args ~@body))
上面的代码没有像我期望的那样创建两个函数。它只会创建最后一个。
user=> (double-it deez [a b] (str b a))
#'user/deez*
如何让一个宏定义两个函数?
答案 0 :(得分:14)
; given a function name, its args and body, create 2 versions:
; ie (double-it foo [] ) should create 2 functions: foo and foo*
(defmacro double-it
[fname args & body]
`(do (defn ~fname ~args ~@body)
(defn ~(symbol (str fname "*")) ~args ~@body)))
(double-it afunc [str] (println str))
(afunc* "asd")
(afunc "asd")
无需单独引用它们。