Fork me on GitHub
#clojure-spec
<
2018-10-05
>
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

4
aisamu19:10:19

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"

taylor20:10:40

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]]

taylor20:10:26

it should also conform []

taylor20:10:29

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

taylor20:10:05

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

aisamu20:10:34

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?

aisamu20:10:02

(and thanks, @U3DAE8HMG!)

👍 4
taylor20:10:35

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

taylor20:10:03

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
aisamu20:10:14

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

👍 4