簡體   English   中英

關於vararg函數的Clojure Spec

[英]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?)

你說這個函數必須接受任意數量的map作為參數並返回一個map。 但是您傳遞給deep-merge-with函數不符合該規范:

(fn [f s] s)

此函數只使用兩個參數,而不是任意數量的映射。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM