Skip to content

Generator[T: T]

[Source]

Wraps a GenObj with composition methods (map, filter, flat_map, union). Shrinking is automatic through choice-sequence replay.

class box Generator[T: T] is
  GenObj[T] box

Implements


Constructors

create

[Source]

new ref create(
  gen: GenObj[T] box)
: Generator[T] ref^

Parameters

Returns


Public Functions

generate

[Source]

Produce a random value from the given source of randomness.

fun box generate(
  rnd: Randomness ref)
: T^ ?

Parameters

Returns

  • T^ ?

filter

[Source]

Only yield values for which predicate returns true.

Rejected values are retried up to 100 times. Each attempt is recorded in its own discardable span so the shrinker can remove the failed draws cleanly. Errors after exhausting retries.

fun box filter(
  predicate: {(T): (T^, Bool)}[T] box)
: Generator[T] box

Parameters

  • predicate: {(T): (T^, Bool)}[T] box

Returns


map[U: U]

[Source]

Apply fn to each generated value.

fun box map[U: U](
  fn: {(T): U^}[T, U] box)
: Generator[U] box

Parameters

  • fn: {(T): U^}[T, U] box

Returns


flat_map[U: U]

[Source]

For each value of this generator, create a generator that is then combined. Both outer and inner choices are in the same sequence, so shrinking the outer value works automatically.

fun box flat_map[U: U](
  fn: {(T): Generator[U]}[T, U] box)
: Generator[U] box

Parameters

  • fn: {(T): Generator[U]}[T, U] box

Returns


union[U: U]

[Source]

Produce the value of this generator or the other with equal probability.

fun box union[U: U](
  other: Generator[U] box)
: Generator[(T | U)] box

Parameters

Returns