Hi,
I would like to generate variable sized map using test.check i.e.
given any generator which generates a map, it should randomly select-keys
from it.
Here's what I've come up with so far:
> (ns proj.util
(:require [clojure.test.check.generators :as gen]
[clojure.math.combinatorics :as combo]))
(defn rand-subset
"Given a collection coll,
it'll generate a random subset."
[coll]
(->> coll
combo/count-subsets
rand-int
(combo/nth-subset coll)))
(defn gen-varying-map
"Given a generator which generates a map,
it'll randomly select keys from it thus making it
varying-sized map.
Note: It can return empty maps as well."
[map-gen]
(gen/fmap (fn [m]
(let [ks (rand-subset (keys m))]
(select-keys m ks)))
map-gen))
Here's an example output,
(gen/sample (gen-varying-map (gen/hash-map
"user" (gen/such-that not-empty
gen/string-alphanumeric)
"level" gen/nat
"timezone" gen/pos-int)))
=>
({"user" "1"}
{"user" "l8", "level" 0, "timezone" 1}
{"level" 1}
{"user" "oA", "timezone" 0}
{"level" 2, "timezone" 1}
{"level" 5}
{"user" "8aP", "level" 5, "timezone" 6}
{"user" "035rqi", "level" 7}
{"timezone" 4}
{"timezone" 2})
My question is, is this the right way? Or is there a better way to do it?
Thanks :)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.