我想知道如何指定一个函数,它有一个参数,在原子中保存一个映射。
(defn do-something [a]
(prn (vals @a)))
这显然不起作用:
(s/fdef do-something
:args (s/cat :a map?))
我如何指定这a
是对地图的引用?
我想知道如何指定一个函数,它有一个参数,在原子中保存一个映射。
(defn do-something [a]
(prn (vals @a)))
这显然不起作用:
(s/fdef do-something
:args (s/cat :a map?))
我如何指定这a
是对地图的引用?
不。clojure.spec 是关于指定数据的结构,原子是状态,而不是数据。并非每个功能都必须具有(或检查)规范。
我处理有状态数据的一般建议是:
稍加注意,您通常可以将获取或返回原子的函数数量减少到 0(通过在管理它的地方关闭原子),这是一个值得的目标。
你没有。出于一个原因,它不是线程安全的。如果您确实以某种方式指定原子包含映射,它可能会在您检查原子并继续执行您的函数的时间内变为整数。
然而,一种选择是为原子提供验证器。您可以轻松地使用 partial 来做到这一点:(set-validator! my-atom (partial s/valid? :my-spec))
. 现在,除非值符合 :my-spec,否则 atom 将无法更新。
另一种选择是向所有更新原子的函数添加验证逻辑。这两种方法中哪一种效果最好取决于应用程序。
您可以使用with-gen
、自定义谓词和自定义生成器:
(require '[clojure.spec.alpha :as spec]
'[clojure.spec.gen.alpha :as gen])
(defn memoize! [memo key distance]
"Memoizes the distance at the given key and returns the distance"
(swap! memo assoc key distance)
distance)
(spec/def ::word
(spec/and string? (complement nil?)))
(defn map-atom? [o]
(and (instance? clojure.lang.IAtom o)
(map? @o)))
(def map-atom-gen
(gen/fmap
(fn [_] (atom {}))
(gen/int)))
(spec/def ::map-atom
(spec/with-gen map-atom?
(constantly map-atom-gen)))
(spec/fdef memoize!
:args (spec/tuple ::map-atom
(spec/coll-of ::word :type vector? :count 2)
nat-int?)
:ret nat-int?
:fn (fn [{[memo key distance] :args, retval :ret}]
(= distance (@memo key) retval)))