s/or creates a spec and s/alt creates a regex op. Regex ops can be combined
to describe a single sequence. Use s/alt if you are matching alternative
regex expressions in a sequential context - in particular where the
branches of the alt are themselves regex ops. Use s/or when matching a
single item. There are indeed cases where you could use one or the other
and even yield the same result, but they have different affordances and
intent when used in combination with other things.
This is an example where s/alt decides between nested regex ops (and s/or
won't work here):
user=> (s/def ::s (s/* (s/alt :key-pair (s/cat :a keyword? :b keyword?)
:nums (s/+ number?))))
:user/s
user=> (s/conform ::s [:a :b 1 2 3 :c :d :e :f 4 5])
[[:key-pair {:a :a, :b :b}] [:nums [1 2 3]] [:key-pair {:a :c, :b :d}]
[:key-pair {:a :e, :b :f}] [:nums [4 5]]]
If we tried this with an s/or, it would try to match a new regular
expression under the or and fail:
user=> (s/def ::s' (s/* (s/or :key-pair (s/cat :a keyword? :b keyword?)
:nums (s/+ number?))))
:user/s'
user=> (s/conform ::s' [:a :b 1 2 3 :c :d :e :f 4 5])
:clojure.spec/invalid
And you would instead match something like this:
user=> (s/conform ::s' [[:a :b] [1 2 3] [:c :d] [:e :f] [4 5]])
[[:key-pair {:a :a, :b :b}] [:nums [1 2 3]] [:key-pair {:a :c, :b :d}]
[:key-pair {:a :e, :b :f}] [:nums [4 5]]]
On Wednesday, May 25, 2016 at 12:48:05 PM UTC-5, Brent Millare wrote:
>
> What's the difference between clojure.spec/or and clojure.spec/alt? They
> seem to accept the same inputs, multiple keyword-predicate pairs, and
> output tagged values when used with clojure.spec/conform. Is
> clojure.spec/or usable within a regular expression?
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.