clojure-spec

About: http://clojure.org/about/spec Guide: http://clojure.org/guides/spec API: https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html
2021-06-22T18:40:50.125500Z

ran into a surprise with s/or just now

2021-06-22T18:40:52.125800Z

;; `s/or` destructures the value that the second pred sees
(let [spec (s/and (s/or :even even? :small #(> 10 %))
                  #(= 5 %))]
  (s/explain-str spec 5))
;; => "[:small 5] - failed: (= 5 %)\n"

;; flipped order of arguments to `s/and
(let [spec (s/and #(= 5 %)
                  (s/or :even even? :small #(> 10 %)))]
  (s/explain-str spec 5))
;; => "Success!\n"

2021-06-22T18:41:34.125900Z

is there a way to avoid this?

2021-06-22T18:44:24.126200Z

ah, i guess it's in the docstring of s/and

๐Ÿ‘ 1
sgepigon 2021-06-22T18:44:39.126400Z

I believe itโ€™s more of s/andโ€™s behavior as it โ€œflowsโ€ values through (see https://github.com/clojure/spec-alpha2/wiki/Differences-from-spec.alpha#nonflowing-sand--new) You can try https://clojuredocs.org/clojure.spec.alpha/nonconforming

๐Ÿ™Œ 1
alexmiller 2021-06-22T18:55:03.126900Z

spec 2 has a non-flowing s/and variant to cover this