关于 vararg 函数的 Clojure 规范

Clojure Spec on a vararg function

我正在尝试为合并函数编写规范,该函数将函数和映射作为输入并使用函数来解决冲突。但是我为该函数编写的规范失败了。我正在尝试弄清楚如何为此类功能编写规范。

下面是代码片段。

(require '[clojure.spec.test :as stest])
(require '[clojure.spec :as spec])

(defn deep-merge-with [func & maps] 
  (let [par-func (partial deep-merge-with func)] 
    (if (every? map? maps) 
        (apply merge-with par-func maps) 
        (apply func maps))))

(spec/fdef deep-merge-with
           :args (spec/cat :func (spec/fspec :args (spec/cat :maps (spec/* map?))
                                             :ret map?)
                           :maps (spec/cat :maps (spec/* map?)))
           :ret map?)

(stest/instrument `deep-merge-with)

(deep-merge-with (fn [f s] s) {:a 1} {:a 2})

我遇到的规范错误是:

clojure.lang.ExceptionInfo: Call to #'boot.user/deep-merge-with did not conform to spec:
    In: [0] val: () fails at: [:args :func] predicate: (apply fn),  Wrong number of args (0) passed to: user/eval22001/fn--22002
                                :clojure.spec/args  (#function[boot.user/eval22001/fn--22002] {:a 1} {:a 2})

在您的 [:args :func] 规范中:

(spec/fspec :args (spec/cat :maps (spec/* map?)) :ret map?)

您是说该函数必须接受 任意数量 个地图和 return 个地图作为参数。但是您传递给 deep-merge-with 的函数不符合该规范:

(fn [f s] s)

此函数正好接受 两个 个参数,而不是任意数量的映射。