That schema looks weird to me as it seems to under-constrain leaf vs branch
so this is not really equivalent but makes sense to me:
(spec/def ::tree (spec/or :leaf ::leaf :branch ::branch))
(spec/def ::leaf integer?)
(spec/def ::branch (spec/cat :left ::tree :right ::tree))
(spec/conform ::tree [[1 2] [3 4]])
=> [:branch {:left [:branch {:left [:leaf 1], :right [:leaf 2]}], :right
[:branch {:left [:leaf 3], :right [:leaf 4]}]}]
Because you register specs in the registry by keyword that gives you the
point of indirection to do recursive structures.
Generate some trees!
(gen/sample (spec/gen ::tree) 5)
=> (-1 -1 (0 0) (0 2) (-1 -1))
(gen/sample (spec/gen ::tree) 5)
=> (((-1 0) 0) 0 1 (0 1) (1 -3))
On Monday, May 23, 2016 at 11:16:44 AM UTC-5, Alex Miller wrote:
>
> Yes, you can create recursive definitions by registering a spec that
> refers to itself via registered name (a namespaced keyword).
>
>
> On Monday, May 23, 2016 at 11:13:24 AM UTC-5, Andrey Grin wrote:
>>
>> Is it planned to support recursive definitions? Example from.plumatic
>> schema:
>>
>>
>> (def BinaryTree
>> (maybe ;; any empty binary tree is represented by nil
>> {:value long
>> :left (recursive #'BinaryTree)
>> :right (recursive #'BinaryTree)}))
>>
>>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.