corda / net.corda.client.mock / Generator

# Generator

`class Generator<out A : Any>`

This file defines a basic Generator library for composing random generators of objects.

An object of type Generator<A> 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.

### Constructors

<init> | `Generator(generate: (SplittableRandom) -> ErrorOr<A>)` This file defines a basic Generator library for composing random generators of objects.

### Properties

generate | `val generate: (SplittableRandom) -> ErrorOr<A>`

### Functions

bind | `fun <B : Any> bind(function: (A) -> Generator<B>): Generator<B>`
combine | `fun <B : Any, R : Any> combine(other1: Generator<B>, function: (A, B) -> R): Generator<R>` `fun <B : Any, C : Any, R : Any> combine(other1: Generator<B>, other2: Generator<C>, function: (A, B, C) -> R): Generator<R>` `fun <B : Any, C : Any, D : Any, R : Any> combine(other1: Generator<B>, other2: Generator<C>, other3: Generator<D>, function: (A, B, C, D) -> R): Generator<R>` `fun <B : Any, C : Any, D : Any, E : Any, R : Any> combine(other1: Generator<B>, other2: Generator<C>, other3: Generator<D>, other4: Generator<E>, function: (A, B, C, D, E) -> R): Generator<R>`
map | `fun <B : Any> map(function: (A) -> B): Generator<B>`
product | `fun <B : Any> product(other: Generator<(A) -> B>): Generator<B>`

### Companion Object Functions

choice | `fun <A : Any> choice(generators: List<Generator<A>>): Generator<A>`
fail | `fun <A : Any> fail(error: Exception): Generator<A>`
frequency | `fun <A : Any> frequency(generators: List<Pair<Double, Generator<A>>>): Generator<A>`
impure | `fun <A : Any> impure(valueClosure: () -> A): Generator<A>`
pure | `fun <A : Any> pure(value: A): Generator<A>`
sequence | `fun <A : Any> sequence(generators: List<Generator<A>>): Generator<List<A>>`
success | `fun <A : Any> success(generate: (SplittableRandom) -> A): Generator<A>`

### Extension Functions

generateOrFail | `fun <A : Any> Generator<A>.generateOrFail(random: SplittableRandom, numberOfTries: Int = 1): A`

### Companion Object Extension Functions

bytes | `fun Generator.Companion.bytes(size: Int): Generator<ByteArray>`
double | `fun Generator.Companion.double(): Generator<Double>`
doubleRange | `fun Generator.Companion.doubleRange(from: Double, to: Double): Generator<Double>`
frequency | `fun <A : Any> Generator.Companion.frequency(vararg generators: Pair<Double, Generator<A>>): Generator<A>`
int | `fun Generator.Companion.int(): Generator<Int>`
intRange | `fun Generator.Companion.intRange(range: IntRange): Generator<Int>` `fun Generator.Companion.intRange(from: Int, to: Int): Generator<Int>`
longRange | `fun Generator.Companion.longRange(range: LongRange): Generator<Long>` `fun Generator.Companion.longRange(from: Long, to: Long): Generator<Long>`
pickN | `fun <A : Any> Generator.Companion.pickN(number: Int, list: List<A>): Generator<List<A>>`
pickOne | `fun <A : Any> Generator.Companion.pickOne(list: List<A>): Generator<A>`
replicate | `fun <A : Any> Generator.Companion.replicate(number: Int, generator: Generator<A>): Generator<List<A>>`
replicatePoisson | `fun <A : Any> Generator.Companion.replicatePoisson(meanSize: Double, generator: Generator<A>): Generator<List<A>>`
sampleBernoulli | `fun <A> Generator.Companion.sampleBernoulli(maxRatio: Double = 1.0, vararg collection: A): Generator<List<Array<out A>>>` `fun <A> Generator.Companion.sampleBernoulli(collection: Collection<A>, maxRatio: Double = 1.0): Generator<List<A>>`