clojure.spec: `alt` vs `or` for sequence spec -
i'm following clojure.spec's guide (http://clojure.org/guides/spec). i'm confused difference between alt
, or
sequence spec.
for me 2 following examples work equally well. what's difference between two?
; use `alt` (s/def ::config (s/* (s/cat :prop string? :val (s/alt :s string? :b boolean?)))) (s/explain ::config ["-server" "foo" "-verbose" true "-user" 13]) ; use `or` (s/def ::config (s/* (s/cat :prop string? :val (s/or :s string? :b boolean?)))) (s/explain ::config ["-server" "foo" "-verbose" true "-user" 13])
s/alt
concatenating nested regex specs using s/or
specifies subsequence. in example doesn't make difference since not using nested regex specs. here example:
(s/def ::number-regex (s/* number?)) (s/def ::or-example (s/cat :nums (s/or :numbers ::number-regex))) (s/valid? ::or-example [1 2 3]) ;;-> false (s/valid? ::or-example [[1 2 3]]) ;;-> true
as can see, or
specifies sub-sequence in new regex context started, whereas alt
specifies opposite:
(s/def ::alt-example (s/cat :nums (s/alt :numbers ::number-regex))) (s/valid? ::alt-example [1 2 3]) ;;-> true (s/valid? ::alt-example [[1 2 3]]) ;;-> false