I was trying to write a generator for a matrix of random elements, but with rows of equal length
so this actually works
(defn gen-image
[nrows ncols]
(gen/vector (gen/vector
(gen/elements core/COLOURS)
ncols)
nrows))
however now gen-image is a function, and also doing this is not possible
(prop/for-all
[nrows gen/pos-int
ncols gen/pos-int
mat (gen-image nrows ncols)])
because nrows and ncols are not defined at that point
I also try to nest prop/for-all but while that doesn't fail it doesn't really seem to do anything useful
any suggestion?
@gfredericks brilliant thanks
err, wasn't there a guide for using recursive-gen
somewhere?
Found it! https://github.com/clojure/test.check/blob/master/doc/intro.md#recursive-generators
Hrm. So I'm a bit stuck trying to write a generator for a directed acyclic graph. This is what I would like to do:
;; generate a list of nodes
;; for each node x in the list
;; generate a none-empty subset of nodes to the right of x in the list
;; for each node in the subset
;; create an edge from x to the node
(gen/bind (gen/list-distinct gen-node {:min-elements 2})
(fn [[node & possible-joins]]
(gen/fmap (fn [adjacent-nodes]
(map (partial join-nodes node) adjacent-nodes))
(gen/set (gen/elements possible-joins)))))
that's what I have so far. Generate some nodes, take the first one, generate a subset from the rest, and create edges between the node and each node in the subset
I'm having trouble figuring out how to recur on the "next" node
you could make that work, but something simpler might be to just generate all the edges at once
(gen/let [nodes (gen/vector-distinct gen-node {:min-elements 2}), edges (gen/vector-distinct (gen/vector-distinct (gen/elements nodes) {:num-elements 2}))] ... figure which direction the edges go based on the ordering of nodes...)
pardon my lack of line breaks