我似乎很难区分 Clojure Spec 的 &
和 and
运算符的含义。它们似乎都做着类似的事情,只是其中一个被标注为正则表达式运算符,这个区别我不确定其重要性。
我似乎很难区分 Clojure Spec 的 &
和 and
运算符的含义。它们似乎都做着类似的事情,只是其中一个被标注为正则表达式运算符,这个区别我不确定其重要性。
如果我们从它们中采样一些数据,就能看到它们之间的差异:
(ns playground
(:require [clojure.spec :as spec]
[clojure.spec.gen :as gen]))
(gen/generate (spec/gen (spec/and #{:a :c} #{:b :a :c})))
=> :a
(gen/sample (spec/gen (spec/and #{:a :c} #{:b :a :c})))
=> (:c :a :c :a :a :a :a :a :a :c)
spec/and
匹配符合两个谓词#{:a :c}
和#{:b :a :c}
的单个出现。(gen/generate (spec/gen (spec/& #{:a :c} #{:b :a :c})))
=> [:c]
(gen/sample (spec/gen (spec/& #{:a :c} #{:b :a :c})))
=> ([:c] [:a] [:a] [:c] [:c] [:c] [:c] [:a] [:c] [:c])
spec/&
另一方面,与谓词作为序列的一部分匹配。