Fork me on GitHub
#clojure-spec
<
2020-05-23
>
David Pham07:05:34

In your answer with with gen/one-of, you repeated keyword? which was the main predicate. In that case you repeated the main predicate twice, once for defining the validity, once in the definition of the generator. Can we avoid this repetition?

David Pham07:05:29

I think it is a common pattern to give some examples to a generator for documentation, but it reduce the power for testing.

niclasnilsson21:05:52

Recursive specs? I’ve tried all old examples I’ve found, but they don’t seem to work (any longer?). In spec or spec2, are recursive specs possible, or is eager lookup making it impossible? I mean things like this:

(def ex '(+ (+ 1 (* 2 3)) 200))

(defn operator? [x] (#{'+ '- '* '/} x))

(s/def ::operator operator?)
(s/def ::operand int?)

;; I've tried many different ways, but fail to get something like this working
(s/def ::expr
  (s/or :operand ::operand
        :expr (s/cat :operator ::operator
                     :left ::expr
                     :right ::expr))