union combines two generators of the same type into one generator, choosing between them with equal likelihood. It turns a boolean coin flip into a generator-selection decision. section 8.1.5 section 8.6
Selecting a generator with a coin flip
def union[A](g1: Gen[A], g2: Gen[A]): Gen[A] = boolean.flatMap(b => if b then g1 else g2) The boolean generator supplies the choice; flatMap uses that choice to select g1 or g2 as the next generator. The resulting Gen[A] preserves the common element type. section 8.6 section 8.1.5
def union[A](g1: Gen[A], g2: Gen[A]): Gen[A]
Inspect the compact union signature before comparing its implementation with the boolean-and-flatMap definition. section 8.1.5
Use union when:
- Two alternative generators produce values of the same type. section 8.1.5
- Each alternative should be selected with equal likelihood rather than custom probabilities. section 8.6
- Selection must remain an explicit, composable generator rather than an immediate random choice. section 8.6
