Fork me on GitHub
#clojure-spec
<
2020-02-24
>
gariepyalex21:02:32

When conforming a sequence spec, can we guaranty key order? For instance:

(s/def ::a neg-int?)
(s/def ::b zero?)
(s/def ::c pos-int?)
(s/def ::sequence (s/cat :a ::a, :(s/? ::b), :c ::c))
I would like to use conform to know if the second element of the sequence is :b or :c (labels given in cat). conform returns a clojure.lang.PersistentHashMap, which does not key the keys in the same order as in the sequence.

Alex Miller (Clojure team)22:02:13

s/tuple is a better match

seancorfield21:02:30

s/conform just tells you how it matched. If you call s/valid? and get true then the input sequence is valid as is.

gariepyalex21:02:19

I would have liked to use conform to parse the sequence and end up with {:a -2, :c 10}

seancorfield22:02:31

Spec isn't really a "parser" but if you know your elements should be in a given order, you can easily manipulate the data you get from s/conform to produce a more appropriate structure.

seancorfield22:02:28

(you know you're going to get :a first and :c last so the only question is (contains? conformed-data :b) to see if a zero is present...

gariepyalex22:02:16

ok thanks for your help!