noprompt / meander

Tools for transparent data transformation
MIT License
923 stars 55 forks source link

Create generators for free #55

Closed jimmyhmiller closed 3 years ago

jimmyhmiller commented 5 years ago

Wouldn't it be great if we could automatically create generators for a pattern and then be able to exercise them automatically?

There are some patterns that we couldn't do this for e.g. (m/pred my-custom-pred-with-no-generator ?x). But thanks to spec allowing generators on preds we should be able to handle those if they have a generator defined. It would be fantastic to automatically see the sorts of data the pattern can match.

Given that this functionality is really not part of doing the matching, we can also use the full power of meander to create these generators, which could be a fun use case.

Example:

(gen/sample
 (create-generator '[1 ?x 2 (m/pred pos-int? ?x)]))

;; =>
([1 1 2 1]
 [1 1 2 1]
 [1 2 2 2]
 [1 1 2 1]
 [1 3 2 3]
 [1 4 2 4]
 [1 28 2 28]
 [1 1 2 1]
 [1 11 2 11]
 [1 33 2 33])