开发者

How do I create a macro to define two functions in clojure

开发者 https://www.devze.com 2023-01-02 07:53 出处:网络
The code below doesn\'t behave as I would expect. ; given a function name, its args and body, create 2 versions:

The code below doesn't behave as I would expect.

; 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))

The code above doesn't create two functions as I would expect. It only creates the last one.

user=> (double-it deez 开发者_如何学运维[a b] (str b a))
#'user/deez*

How can I get a single macro to define two functions?



; 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")

No need to quote them separately.

0

精彩评论

暂无评论...
验证码 换一张
取 消