I had a situation where I had an existing function that generated examples of a particular type x:
(defn generate-sample-of-x [] ... )
and I wanted to use that inside of spec as a generator. I could not find a built in way to do that. In the end I created my own helper function for it:
(defn- generator-from-fn "Create a spec generator from a no-arg function"
[f] (gen/fmap (fn [_] (f)) (gen/return 0)))
which can be used to produce a generator from any zero-arg function. For example:
(generator-from-fn generate-sample-of-x)
I was wondering two things:
- Is there something built-in for this that I just failed to find?
- If not, is there a simpler way to define generator-from-fn, or is what I have about as simple as it can be made?
thanks