【发布时间】:2017-09-29 17:29:15
【问题描述】:
假设我们有一个函数get-ints,它有一个位置参数、调用者想要的整数数量以及两个命名参数:max 和:min,例如:
; Ignore that the implementation of the function is incorrect.
(defn get-ints [nr & {:keys [max min] :or {max 10 min 0}}]
(take nr (repeatedly #(int (+ (* (rand) (- max min -1)) min)))))
(get-ints 5) ; => (8 4 10 5 5)
(get-ints 5 :max 100) ; => (78 43 32 66 6)
(get-ints 5 :min 5) ; => (10 5 9 9 9)
(get-ints 5 :min 5 :max 6) ; => (5 5 6 6 5)
如何为get-ints 的参数列表编写 Plumatic Schema,一个、三个或五个项目的列表,其中第一个始终是数字,后面的项目始终是关键字和关联值的对.
使用 Clojure Spec 我将其表达为:
(require '[clojure.spec :as spec])
(spec/cat :nr pos-int? :args (spec/keys* :opt-un [::min ::max]))
以及::min 和::max 持有的有效值的单独定义。
【问题讨论】: