clojure-spec 2021-09-06

@alexmiller just curious: any progress on spec2 lately?

👍 1
Alex Miller (Clojure team) 2021-09-06T16:05:40.048900Z

no

😞 3

I'd like to model a binary tree with a map of the form {:payload 0 :left sub1 :right sub2}. Here's my spec for now:

(s/def ::binary-tree-2
  (s/with-gen
    (s/keys :req-un [::payload]
            :opt-un [::left ::right])
    #(gen/recursive-gen
      (fn [f] (gen/let [[val l r] (gen/tuple (s/gen int?) f f)]
               (merge
                {:payload val}
                (when l {:left l})
                (when r {:right r}))))
      (gen/return nil))))
But this doesn't check that ::left and ::right have the correct shape. What should I do instead?

I added more predicates with s/and, but I still wonder if there's a better way

I was wondering something similar a while back, and happened to find this: https://deque.blog/2017/05/20/a-clojure-spec-for-generic-binary-trees/ perhaps it might help?