Fork me on GitHub
#clojure-spec
<
2021-04-12
>
Carlo09:04:53

what would be the most idiomatic way to spec a graph structure? I would want something around (`graph` is the loom library):

(s/def ::my-graph
      (s/and
       graph/directed?
       #(not= :cljs.spec.alpha/invalid (s/conform (s/coll-of ::my-node) (graph/nodes %)))))

Carlo09:04:13

but I find that predicate quite cumbersome to write

Carlo09:04:58

(s/def ::my-graph
      (s/and
       graph/directed?
       #(s/valid? (s/coll-of ::my-node) (graph/nodes %))))
is a bit better. Is there anything better still?

borkdude10:04:27

@meditans not an answer to your question, but in general: instead of = :cljs.spec.alpha/invalid use (s/invalid? ...)

🙌 4
borkdude10:04:30

I think you should be able to leave out s/valid? and just pass a spec

Carlo11:04:58

but how would I also invoke graph/nodes ?

em18:04:54

What does your graph data structure look like? Is it some kind of custom data type, and therefore it requires the graph/nodes accessor?

em18:04:24

@meditans If it's just a map, which is preferred in most situations, you can just spec a :graph/nodes key while also getting all the benefits of spec for your other graph metadata

Carlo19:04:10

the data structure is the one in the loom library. (graph/nodes %) is the invocation of the function that gives the nodes back

colinkahn20:04:51

One potential option is to use a conformer in your s/and to handle the conversion into your specable form: https://clojuredocs.org/clojure.spec.alpha/conformer

(s/and graph/directed?
       (s/conformer graph/nodes)
       (s/coll-of ::my-node))