Fork me on GitHub
#clojure-spec
<
2021-05-04
>
Noah Bogart15:05:17

i'm looking to write a spec that checks a list of tuples to see if there's 0 or 1 tuple that starts with some value

Noah Bogart15:05:12

i have the spec that verifies and conforms that the shape is correct:

(s/def ::handler-clause (s/cat :name (s/nonconforming
                                       (s/or :keyword keyword?
                                             :class symbol?))
                               :arglist vector?
                               :body (s/* any?)))

Noah Bogart15:05:42

and then in the fdef i have (s/* (s/spec ::handler-clause)) for that argument

Noah Bogart15:05:20

inputs look like [:error [& args] (println args)] or [:no-error [& args] args]

borkdude15:05:30

* means 0 or more, not 0 or 1

Noah Bogart15:05:13

my apologies, i mean, i'd like to have it check that there is any number of tuples that don't start with :no-error and 0 or 1 tuples that start with :no-error

Noah Bogart15:05:11

so something like

(s/fdef handler-case
  :args (s/cat :expr any?
               :bindings (s/and (s/? (s/cat :name (s/and #(= :no-error %)
                                                         (s/nonconforming
                                                           (s/or :keyword keyword?
                                                                 :class symbol?)))
                                            :arglist vector?
                                            :body (s/* any?)))
                                (s/* (s/cat :name (s/and #(not= :no-error %)
                                                         (s/nonconforming
                                                           (s/or :keyword keyword?
                                                                 :class symbol?)))
                                            :arglist vector?
                                            :body (s/* any?)))
                           )))