This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-28
Channels
- # bangalore-clj (1)
- # beginners (67)
- # braveandtrue (179)
- # cider (28)
- # cljdoc (1)
- # clojure (132)
- # clojure-conj (3)
- # clojure-dev (1)
- # clojure-finland (6)
- # clojure-nl (2)
- # clojure-russia (6)
- # clojure-spec (19)
- # clojure-uk (62)
- # clojurescript (90)
- # clojutre (5)
- # component (2)
- # cursive (30)
- # data-science (1)
- # datomic (42)
- # duct (9)
- # emacs (1)
- # figwheel-main (158)
- # fulcro (57)
- # funcool (3)
- # hoplon (1)
- # jobs (17)
- # mount (38)
- # off-topic (15)
- # re-frame (53)
- # remote-jobs (2)
- # schema (11)
- # shadow-cljs (299)
- # spacemacs (25)
- # specter (2)
- # tools-deps (54)
- # vim (11)
- # yada (6)
You control what goes into that atom, and when. After all, global spec registry is an atom too.
I do not understand this mismatch, as I see it. Do you know the reason why :gen
takes a function and not a generator? (s/exercise boolean? 1 gen/boolean)
ok but (s/exercise (s/spec boolean? :gen gen/boolean) 1)
bad. So I write (s/exercise (s/spec boolean? :gen (constantly gen/boolean)) 1)
. Is there a better way?
https://www.dropbox.com/s/plwu44v61adscap/Screenshot%202018-08-28%2010.39.11.png?dl=0 The first s/spec
form works for me, using (constantly gen/boolean)
does not...
Specs defer gens via functions to avoid loading clojure.spec.gen.alpha when not necessary. When you call exercise, it takes an overrides map which is a map of spec name to function returning generator.
(s/exercise boolean? 1 gen/boolean)
is not ok, that last gen/boolean is just not being used (boolean? has a built-in generator)
I usually do something like (s/exercise (s/spec boolean? :gen #(gen/boolean)))
I suspect something is wacky in the macrology that’s making the constantly version not work there
@alexmiller I'm curious as to why the unwrapped version worked in my REPL then...?
unwrapped version of?
https://www.dropbox.com/s/3y3bm4hjvipthhp/Screenshot%202018-08-28%2011.40.39.png?dl=0
Seems to work both with and without the #( ... )
around gen/boolean
...
not sure - since this one has a built-in, that might be masking something too
oh, so gen/boolean (where gen is clojure.spec.gen.alpha) is actually already a thunk
so gen/boolean works as is and #(gen/boolean) is a function that invokes gen/boolean and returns the generator, so is exactly the same thing
but (constantly gen/boolean) is a function that returns gen/boolean not invoked, which is a function rather than a generator
so going way back to the top @ulrich.becker said “(s/exercise (s/spec boolean? :gen gen/boolean) 1) is bad”, but it’s not bad - that’s exactly what you want here (the missing piece of information being that clojure.spec.gen.alpha already wraps gens in thunks
Ah!! 💡 Now it all makes sense. That also explains why this did not work: (s/exercise (s/spec boolean? :gen (fn [] (println "Gen!") gen/boolean)) 1)
(I wanted to see how often the thunk was called and when I corrected it to call (gen/boolean)
the answer was "just once", regardless of the exercise
size)