Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
clojure中的函数定义宏_Clojure_Clojurescript - Fatal编程技术网

clojure中的函数定义宏

clojure中的函数定义宏,clojure,clojurescript,Clojure,Clojurescript,我正试图制作一个宏来改变这一点: (defn-check my-checked-function check-function [a A b B] (do-something a b)) 进入: 我是clojure的新手,谁能教我怎么做 (defmacro defn-check [name check-fn args & tail] (let [docstring (if (string? (first tail)) (first tail)) t

我正试图制作一个宏来改变这一点:

(defn-check my-checked-function
  check-function
  [a A
   b B]
  (do-something a b))
进入:

我是clojure的新手,谁能教我怎么做

(defmacro defn-check [name check-fn args & tail]
  (let [docstring (if (string? (first tail)) (first tail))
        tail (if docstring (next tail) tail)
        params (take-nth 2 args)
        checks (take-nth 2 (next args))
        cf (gensym "cf__")]
    `(let [~cf ~check-fn]
       (defn ~name
         ~@(if docstring [docstring])
          ~(vec params)
         {:pre ~(mapv (fn [p c]
                        `(= (~cf ~p) ~c))
                      params
                      checks)}
         ~@tail))))
这允许您根据需要指定docstring,但不允许指定属性映射(请参见
(doc defn)
;添加对此的支持将非常简单)

例如:

user> (defn-check funky-add odd? [x true y false] "foo" (+ x y))
#'user/funky-add
user> (doc funky-add)
-------------------------
user/funky-add
([x y])
  foo
nil
user> (funky-add 1 2)
3
user> (funky-add 2 1)
AssertionError Assert failed: (clojure.core/= (cf__2268 x) true)  user/eval2269/funky-add--2270 (form-init1446120099766722611.clj:1)
user> (defn-check funky-add odd? [x true y false] "foo" (+ x y))
#'user/funky-add
user> (doc funky-add)
-------------------------
user/funky-add
([x y])
  foo
nil
user> (funky-add 1 2)
3
user> (funky-add 2 1)
AssertionError Assert failed: (clojure.core/= (cf__2268 x) true)  user/eval2269/funky-add--2270 (form-init1446120099766722611.clj:1)