Fork me on GitHub
#clojure-spec
<
2021-09-06
>
mpenet16:09:25

@alexmiller just curious: any progress on spec2 lately?

👍 2
Carlo16:09:49

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?

Carlo17:09:44

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

Bobbi Towers20:09:24

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?