Fork me on GitHub
Alex Miller (Clojure team)02:10:32

you don’t have to require the namespace

Alex Miller (Clojure team)02:10:48

fdef creates an entry in the spec registry keyed by the namespaced symbol, but you don’t need to load that namespace at that point


Wrapping a cat with an and is breaking my spec in interesting ways. What am I missing?

(s/def ::pair
  (s/cat :first (s/nilable number?)
         :second (s/nilable number?)))

(s/conform ::pair [1 2])
;; => {:first 1, :second 2}
(s/conform (s/? ::pair) [1 2])
;; => {:first 1, :second 2}

(s/def ::ordered-pair
  (s/and ::pair
         #(<= (:first %) (:second %))))

(s/conform ::ordered-pair [1 2])
;; => {:first 1, :second 2}
(s/conform (s/? ::ordered-pair) [1 2])
;; => :clojure.spec.alpha/invalid
(s/explain-str (s/? ::ordered-pair) [1 2])
;; => "In: [0] val: 1 fails spec: :apij.unit.specs.models.rate/pair predicate: (cat :first (nilable number?) :second (nilable number?))\n"


the s/? isn't "flattening" the regex spec (`s/cat`) because it's wrapped in s/and. I think those last tests would conform [[1 2]]


it should also conform []


(s/? ::ordered-pair) is becoming "match a sequence of zero-or-one ::ordered-pairs"


if you wanted a something-or-nothing spec you could try (s/nilable ::ordered-pair) too but I'm not sure of your use case


Yup, both cases conform correctly! I'm mostly trying to understand the non-flattening, though. Is there an alternative construct to use the ordering function without using s/and?


(and thanks, @U3DAE8HMG!)

👍 4

hmm what would multiple pair inputs look like for you? one big sequence like [1 2 3 4] or a sequence of pair seqs like [[1 2] [3 4]]

Alex Miller (Clojure team)20:10:22

s/and converts a regex spec to a non-regex. s/& is a regex version

👍 12
☝️ 8
Alex Miller (Clojure team)20:10:53

will still continue operating at the same “level” of sequence


ah yeah, you can just replace s/and with s/& in ::ordered-pair if you want to conform inputs like [1 2 3 4]

👌 4

Oh,facepalm Not only it makes perfect sense, but it's pretty obvious in retrospect! Thanks @U064X3EF3 and @U3DAE8HMG!

👍 4