What is the difference between `&` and `and` in Clojure Spec?

It seems to me hard to contain the meaning of the & and and Clojure Spec statements. Both of them seem similar to the fact that one of them is referred to as a regular expression operator, the difference is I'm not sure I understand the importance.

+5
source share
1 answer

We can see the difference between the two if we select 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 corresponds to single occurrences matching 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 is accepted by predicates as part of a sequence.

+6
source

Source: https://habr.com/ru/post/1266157/


All Articles