public class Generator<A>
This file defines a basic class Generator
library for composing random generators of objects.
An object of type class Generator
captures a generator of As. Generators may be composed in several ways.
Generator.choice picks a generator from the specified list and runs that. Generator.frequency is similar to choice but the probability may be specified for each generator (it is normalised before picking). Generator.combine combines two generators of A and B with a function (A, B) -> C. Variants exist for other arities. Generator.bind sequences two generators using an arbitrary A->Generator function. Keep the usage of this function minimal as it may explode the stack, especially when using recursion.
There are other utilities as well, the type of which are usually descriptive.
Example: val birdNameGenerator = Generator.pickOne(listOf("raven", "pigeon")) val birdHeightGenerator = Generator.doubleRange(from = 10.0, to = 30.0) val birdGenerator = birdNameGenerator.combine(birdHeightGenerator) { name, height -> Bird(name, height) } val birdsGenerator = Generator.replicate(2, birdGenerator) val mammalsGenerator = Generator.sampleBernoulli(listOf(Mammal("fox"), Mammal("elephant"))) val animalsGenerator = Generator.frequency( 0.2 to birdsGenerator, 0.8 to mammalsGenerator ) val animals = animalsGenerator.generate(SplittableRandom()).getOrThrow()
The above will generate a random list of animals.
class Generator
,
class Generator
Modifier and Type | Class and Description |
---|---|
static class |
Generator.Companion |
Modifier and Type | Field and Description |
---|---|
static Generator.Companion |
Companion |
Constructor and Description |
---|
Generator(kotlin.jvm.functions.Function1<? super java.util.SplittableRandom,? extends net.corda.core.ErrorOr<? extends A>> generate)
This file defines a basic
class Generator library for composing random generators of objects. |
Modifier and Type | Method and Description |
---|---|
<B> Generator<B> |
bind(kotlin.jvm.functions.Function1<? super A,? extends net.corda.client.mock.Generator<? extends B>> function) |
<B,R> Generator<R> |
combine(Generator<? extends B> other1,
kotlin.jvm.functions.Function2<? super A,? super B,? extends R> function) |
<B,C,R> Generator<R> |
combine(Generator<? extends B> other1,
Generator<? extends C> other2,
kotlin.jvm.functions.Function3<? super A,? super B,? super C,? extends R> function) |
<B,C,D,R> Generator<R> |
combine(Generator<? extends B> other1,
Generator<? extends C> other2,
Generator<? extends D> other3,
kotlin.jvm.functions.Function4<? super A,? super B,? super C,? super D,? extends R> function) |
<B,C,D,E,R> |
combine(Generator<? extends B> other1,
Generator<? extends C> other2,
Generator<? extends D> other3,
Generator<? extends E> other4,
kotlin.jvm.functions.Function5<? super A,? super B,? super C,? super D,? super E,? extends R> function) |
kotlin.jvm.functions.Function1<java.util.SplittableRandom,net.corda.core.ErrorOr> |
getGenerate() |
<B> Generator<B> |
map(kotlin.jvm.functions.Function1<? super A,? extends B> function) |
<B> Generator<B> |
product(Generator<? extends kotlin.jvm.functions.Function1<? super A,? extends B>> other) |
public static Generator.Companion Companion
public Generator(kotlin.jvm.functions.Function1<? super java.util.SplittableRandom,? extends net.corda.core.ErrorOr<? extends A>> generate)
This file defines a basic class Generator
library for composing random generators of objects.
An object of type class Generator
captures a generator of As. Generators may be composed in several ways.
Generator.choice picks a generator from the specified list and runs that. Generator.frequency is similar to choice but the probability may be specified for each generator (it is normalised before picking). Generator.combine combines two generators of A and B with a function (A, B) -> C. Variants exist for other arities. Generator.bind sequences two generators using an arbitrary A->Generator function. Keep the usage of this function minimal as it may explode the stack, especially when using recursion.
There are other utilities as well, the type of which are usually descriptive.
Example: val birdNameGenerator = Generator.pickOne(listOf("raven", "pigeon")) val birdHeightGenerator = Generator.doubleRange(from = 10.0, to = 30.0) val birdGenerator = birdNameGenerator.combine(birdHeightGenerator) { name, height -> Bird(name, height) } val birdsGenerator = Generator.replicate(2, birdGenerator) val mammalsGenerator = Generator.sampleBernoulli(listOf(Mammal("fox"), Mammal("elephant"))) val animalsGenerator = Generator.frequency( 0.2 to birdsGenerator, 0.8 to mammalsGenerator ) val animals = animalsGenerator.generate(SplittableRandom()).getOrThrow()
The above will generate a random list of animals.
class Generator
,
class Generator
public <B> Generator<B> map(kotlin.jvm.functions.Function1<? super A,? extends B> function)
public <B> Generator<B> product(Generator<? extends kotlin.jvm.functions.Function1<? super A,? extends B>> other)
public <B,R> Generator<R> combine(Generator<? extends B> other1, kotlin.jvm.functions.Function2<? super A,? super B,? extends R> function)
public <B,C,R> Generator<R> combine(Generator<? extends B> other1, Generator<? extends C> other2, kotlin.jvm.functions.Function3<? super A,? super B,? super C,? extends R> function)
public <B,C,D,R> Generator<R> combine(Generator<? extends B> other1, Generator<? extends C> other2, Generator<? extends D> other3, kotlin.jvm.functions.Function4<? super A,? super B,? super C,? super D,? extends R> function)
public <B,C,D,E,R> Generator<R> combine(Generator<? extends B> other1, Generator<? extends C> other2, Generator<? extends D> other3, Generator<? extends E> other4, kotlin.jvm.functions.Function5<? super A,? super B,? super C,? super D,? super E,? extends R> function)
public <B> Generator<B> bind(kotlin.jvm.functions.Function1<? super A,? extends net.corda.client.mock.Generator<? extends B>> function)
public kotlin.jvm.functions.Function1<java.util.SplittableRandom,net.corda.core.ErrorOr> getGenerate()