Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify documentation for merging #2888

Merged
merged 1 commit into from Mar 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions documentation/docs/proptest/genops.md
Expand Up @@ -54,23 +54,27 @@ val dependentArbs: Arb<String> = Arb.of("foo", "bar").flatMap { prefix ->

## Merging

Two generators can be merged together, so that elements 0, 2, 4, ... are taken from the first generator, and elements 1, 3, 5, ... are taken from the second generator.
Two generators can be merged together, so that the resulting elements are equally sampled from both generators.

```kotlin
val merged = arbA.merge(arbB)
```

So with the following example:
So with the following example would have an equal chance to yield either `"a"` or `"b"` on each random sample:

```kotlin
val a = arbitrary { "a" }
val b = arbitrary { "b" }
val ab = a.merge(b)
ab.take(10).forEach { println(it) }
```

Would ouput `ababababab`
println(ab.take(1000).groupingBy { it }.eachCount())
// {a=493, b=507}
```

For merging more than two arbitraries, `Arb.choice` or `Arb.choose` might be more appropriate. For instance we can use:
- `Arb.choice(arbA, arbB, arbC)` for uniform sampling between `arbA`, `arbB` and `arbC`,
- or `Arb.choose(4 to arbA, 1 to arbB, 5 to arbC)` for a more granular control of frequency of each arbitrary.
In this example `arbA`, `arbB`, and `arbC` will be sampled 40%, 10%, and 50% of the time, respectively.

## Bind

Expand Down