# Generator\[T: T\]
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-16)</span>

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


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

#### Implements

* [GenObj](pony_check-GenObj.md)\[T\] box

---

## Constructors

### create
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-23)</span>


```pony
new ref create(
  gen: GenObj[T] box)
: Generator[T] ref^
```
#### Parameters

*   gen: [GenObj](pony_check-GenObj.md)\[T\] box

#### Returns

* [Generator](pony_check-Generator.md)\[T\] ref^

---

## Public Functions

### generate
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-26)</span>


Produce a random value from the given source of randomness.


```pony
fun box generate(
  rnd: Randomness ref)
: T^ ?
```
#### Parameters

*   rnd: [Randomness](pony_check-Randomness.md) ref

#### Returns

* T^ ?

---

### filter
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-32)</span>


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.


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

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

#### Returns

* [Generator](pony_check-Generator.md)\[T\] box

---

### map\[U: U\]
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-58)</span>


Apply `fn` to each generated value.


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

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

#### Returns

* [Generator](pony_check-Generator.md)\[U\] box

---

### flat_map\[U: U\]
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-68)</span>


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.


```pony
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

* [Generator](pony_check-Generator.md)\[U\] box

---

### union\[U: U\]
<span class="source-link">[[Source]](src/pony_check/gen_obj.md#L-0-81)</span>


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


```pony
fun box union[U: U](
  other: Generator[U] box)
: Generator[(T | U)] box
```
#### Parameters

*   other: [Generator](pony_check-Generator.md)\[U\] box

#### Returns

* [Generator](pony_check-Generator.md)\[(T | U)\] box

---

