Skip to content

Disjunctive or, but with weights #4

@lvh

Description

@lvh

Sometimes, to generate realistic specs, some specs should be more probable than others.

Suggested implementation:

(defmacro weighted-or
  "Like [[s/or]], but with weights."
  [& weighted-options]
  (let [[names specs ws] (apply map vector (partition 3 weighted-options))
        sums (reductions + ws)]
    `(s/with-gen
       (s/or ~@(interleave names specs))
       (fn []
         (gen/fmap
          (fn [count#] (condp < count# ~@(interleave sums specs)))
          (gen/choose 1 ~(last sums)))))))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions