Fork me on GitHub
#test-check
<
2019-02-11
>
Ajay15:02:41

hi, is it possible to make a generator from a custom lazy sequence? For practice, I was trying to use test-check to test FizzBuzz problem - I want a generator that returns multiples of 3 but not of 5 or 15

gfredericks15:02:49

gen/such-that would suffice here for casual use

Ajay15:02:31

but it gave up when I tried

gfredericks15:02:08

Give it a higher max-tries, like 100

Alex Miller (Clojure team)15:02:44

it’s help to invert your thinking for stuff like this

Alex Miller (Clojure team)15:02:08

generate a number, then use fmap to multiply by 3 so you always generate multiples of 3

Alex Miller (Clojure team)15:02:32

could then use such-that over that to exclude multiples of 5 if needed

Ajay15:02:33

Ok I had tried generate a number, then use fmap to multiply by 3 so you always generate multiples of 3 but didn't apply such-that to exclude multiples of 5, let me try, thank you for all replies!

gfredericks15:02:38

yeah I forgot to say the multiply part

gfredericks15:02:59

you could still use such-that without multiplying, it'd just be even slower and you'd probable need an even higher max-tries

gfredericks15:02:01

if you want to be fancy you can use some number theory to generate a good distribution of such numbers w/o filtering

Ajay15:02:46

could get it to work! thank you!

👍 5
Alex Miller (Clojure team)16:02:38

you could gen a set of non-5 numbers, then multiply all of them and by 3 to generate only numbers divisible by 3 but not 5 :)

5
gfredericks16:02:08

@alexmiller that's just moving the filtering to the first generator, right?

gfredericks16:02:17

the fancy thing I have in mind is basically (gen/let [fifteens gen/int, threes (gen/choose 1 4)] (+ (* 15 fifteens) (* 3 threes))) I think that'd work