This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-06-16
Channels
- # announcements (2)
- # beginners (25)
- # calva (50)
- # cider (33)
- # clj-kondo (46)
- # cljs-dev (5)
- # cljsrn (23)
- # clojure (34)
- # clojure-dev (5)
- # clojure-russia (2)
- # clojure-spec (29)
- # clojurescript (11)
- # datomic (3)
- # docker (2)
- # duct (1)
- # emacs (11)
- # fulcro (15)
- # jobs-discuss (47)
- # off-topic (11)
- # om (2)
- # pathom (6)
- # re-frame (9)
- # reitit (4)
- # remote-jobs (1)
- # shadow-cljs (105)
- # spacemacs (4)
- # tools-deps (6)
- # xtdb (4)
how is it possible to prevent a generator from doing a retry without re-calling a generator function? Here's an ugly phone number generator
(defn phone-number-generator []
(gen/fmap (fn [[cc phone-num]]
(str "+" cc " (0) " phone-num))
(gen/tuple (gen/return (rand-nth ["1" "37" "39" "44" "49"]))
(gen/return (-> (* Integer/MAX_VALUE (Math/random))
int
str)))))
printing the validation from the spec
"VALID?" "+1 (0) 265957916" false
it gets print 100 times exactly the same, the generator is only called 1x or 2x
that all ends with
Execution error (ExceptionInfo) at clojure.test.check.generators/such-that-helper (generators.cljc:320).
Couldn't satisfy such-that predicate after 100 tries.
@hlolli generally you design the generator so that it has no need to retry
I can't see the spec so I don't know what that would be
I see, it's this here
(defn-spec phone-number-valid? boolean?
[phone-number string?]
(let [instance (PhoneNumberUtil/getInstance)
phone-number-parsed (.parse instance phone-number "")]
(.isValidNumber instance phone-number-parsed)))
(s/def :user/phone-number ; format
(s/with-gen phone-number-valid?
generators/phone-number-generator))
okay well there's some stuff buried in the jvm method but for example, is the last portion supposed to be a particular number of digits?
yes, it depends I think which country code is used, so I just multiply some random number to sizeof(int), should give me a more likely way of number that purely random number generator, I think?
(* Integer/MAX_VALUE (Math/random))
is going to be pretty skewed towards certain lengths
if it's easy to get a length associated with each country code, then you could do it much better
yes, these rules are difficuly, despite all lengths being right, sometimes the first digit must fit some standard
(gen/for [[country-code length] (gen/elements country-codes-and-lengths)]
number (apply gen/tuple (repeat length (gen/choose 0 9)))]
(str "+" country-code " (0) " number))
is the sort of thing I would aim for if I Had that listrelying on the generator trying again is not a good plan unless you're sure it's highly likely to succeed
because you can easily be in a situation where it only succeeds e.g. 0.001% of the time
and then you'll be burning a lot of CPU
yes true, I thought the idea would be to give the generator a bit of help. The odds of alpha-numberic random gen hitting a valid phone number are probably astronomical, I'd assume
yeah, you can definitely do worse than what you have 🙂
@hlolli incidentally, here's a generator that's evenly distributed between digit lengths
(gen/for [digit-count (gen/choose 5/10)
digits (apply gen/tuple (repeat digit-count (gen/choose 0 9)))]
digits)
that might work better for you
clojure.test.check.generators
it's just syntax sugar for gen/fmap
and gen/bind
mostly
you can do the above just using gen/bind