This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-04
Channels
- # adventofcode (129)
- # announcements (1)
- # babashka (7)
- # beginners (30)
- # calva (42)
- # cider (2)
- # clj-commons (2)
- # clj-on-windows (27)
- # clj-yaml (4)
- # clojure (69)
- # clojure-belgium (4)
- # clojure-europe (5)
- # clojure-norway (4)
- # clr (1)
- # css (3)
- # datomic (19)
- # dev-tooling (2)
- # events (1)
- # humbleui (1)
- # hyperfiddle (17)
- # introduce-yourself (4)
- # java (1)
- # joyride (3)
- # kaocha (1)
- # lsp (4)
- # malli (10)
- # mount (1)
- # nbb (34)
- # off-topic (37)
- # pathom (1)
- # ring (4)
- # tools-deps (12)
A question about recursive generators and :+
. Consider this recursive spec that describes a boolean formula:
(def formula
[:schema
{:registry
{::formula
[:or
:boolean
[:tuple [:enum :not] :boolean]
[:tuple [:enum :and] [:* [:ref ::formula]]]
[:tuple [:enum :or] [:* [:ref ::formula]]]]}}
[:ref ::formula]])
We can of course generate examples via:
(repeatedly 20 #(mg/generate formula))
But, if I change :*
with :+
(rest in 🧵)I now have:
(def formula
[:schema
{:registry
{::formula
[:or
:boolean
[:tuple [:enum :not] :boolean]
[:tuple [:enum :and] [:+ [:ref ::formula]]]
[:tuple [:enum :or] [:+ [:ref ::formula]]]]}}
[:ref ::formula]])
(repeatedly 20 #(mg/generate formula))
and the generator for this seems broken to me (for starters, it usually doesn't return 20 things. I suspect the reason is that it makes more difficult to generate values (as a naive sampling terminates more rarely). Is that correct?recursive generators are hard, please read https://github.com/metosin/malli/blob/master/src/malli/generator.cljc#L186-L281. Ideas welcome on how to make it better.
FYI I'm looking into it here: https://github.com/metosin/malli/pull/792
the issue is that recursive :+
can make generators like (gen/non-empty (gen/return ()))
That was a quick resolution, thanks @U055XFK8V!
Also, what's :malli.core/potentially-recursive-seqex
supposed to represent? Can't seqexes contain recursive terms?
By a brute force search, I saw that this problem is solved if I wrap :+
into a :schema
. A more thorough explanation would be useful though
Can someone confirm whether :schema
values being explained as reified values is a bug?
malli.core
> (explain [:map [:a int?]] {:a "_"})
{:errors ({:in [:a],
:path [:a],
:schema #<malli.core$_simple_schema$reify$reify__13042@17a463a5>,
:value "_"}),
:schema #<malli.core$_map_schema$reify$reify__13253@3a3e779a>,
:value {:a "_"}}
https://github.com/metosin/malli/issues/789