Using custom generators

55 views
Skip to first unread message

Andreas Liljeqvist

unread,
May 3, 2016, 5:09:04 AM5/3/16
to Plumbing and Graph: the Clojure utility belt
Hi.

I can use the following link for generating a BigDecimal.


How can I provide a custom generator for :a ?

{:a BigDecimal
 :b BigDecimal}

(there might be a lot more than two keys)

Thanks for any help.

Jason Wolfe

unread,
May 3, 2016, 7:37:10 AM5/3/16
to Andreas Liljeqvist, Plumbing and Graph: the Clojure utility belt
Once you add the BigDecimal generator, it should automatically work to
generate from the composite schema you provide. Or do you mean
something else? If so, can you please provide a concrete example of
the schema and what you want to generate from it?

Thanks,
Jason
> --
> You received this message because you are subscribed to the Google Groups
> "Plumbing and Graph: the Clojure utility belt" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to prismatic-plumb...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Andreas Liljeqvist

unread,
May 3, 2016, 8:05:29 AM5/3/16
to Plumbing and Graph: the Clojure utility belt, and...@aldb.se
(def data {:a BigDecimal
                :b BigDecimal}

(c/complete {} data {} {BigDecimal (clojure.test.check.generators/fmap #(BigDecimal. (str %)) clojure.test.check.generators/double) })

If we apply a leaf-generator for BigDecimal, then all BigDecimals will use that.

I want to use custom generators for specific BigDecimal keys.
For example :a should always be negative and :b should always be positive.
This is a case of where we might use different types, but the constraints is only for generating a demo.

The types are actually BigDecimal, but some generated results are a bit improbable.
Does that make sense?

Jason Wolfe

unread,
May 3, 2016, 8:55:16 AM5/3/16
to Andreas Liljeqvist, Plumbing and Graph: the Clojure utility belt
That makes sense, thanks.

To do this as-is, you will either have to provide a leaf generator for
`data` that generates the whole thing in one shot, or use a wrapper
(e.g. `(generators/fmap (fn [d] (update-in d :a #(Math/abs %))))`) to
transform the values . You can find an example of this in the
generators test.

If it's possible, a better approach might be to make your schema more
semantic, like:

(def PositiveDecimal (s/constrained BigDecimal pos?))
(def NegativeDecimal (s/constrained BigDecimal neg?))

(def data {:a PositiveDecimal :b NegativeDecimal}).

Then you might not even need to use custom generators (if the
constraints work as intended), but if desired you can just hang custom
generators on PositiveDecimal and NegativeDecimal, etc. Does that
make sense?
>> > email to prismatic-plumb...@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Plumbing and Graph: the Clojure utility belt" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to prismatic-plumb...@googlegroups.com.

Andreas Liljeqvist

unread,
May 11, 2016, 8:50:50 AM5/11/16
to Jason Wolfe, Plumbing and Graph: the Clojure utility belt
Hi.

Sorry for my late reply.
It does make a lot of sense.

Thank you for your help.
--
Mvh

Andreas Liljeqvist
Systemutvecklare
ALDB Concrete Solutions AB
Reply all
Reply to author
Forward
0 new messages