I seem to have a hard time keeping apart the meaning of the &
and and
operators of Clojure Spec. They both seem to do sort of the same thing, only one is noted as a regex operator, a difference I'm not sure I understand the importance of.
We can see the difference between the two if we sample some data from them:
(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)
As we can see spec/and
matches single occurrences of what matches the two predicates #{:a :c}
and #{: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/&
on the other hand matches what's accepted by the predicates as part of a sequence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With