Method Generate
Generate<TResult>(TResult, Func<TResult, ValueTask<TResult>>)
Returns a sequence of values consecutively generated by a generator function.
Declaration
public static IAsyncEnumerable<TResult> Generate<TResult>(TResult initial, Func<TResult, ValueTask<TResult>> generator)
Parameters
| Type | Name | Description |
|---|---|---|
| TResult | initial | Value of first element in sequence |
| Func<TResult, ValueTask<TResult>> | generator | Generator function which takes the previous series element and uses it to generate the next element. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<TResult> | A sequence containing the generated values. |
Type Parameters
| Name | Description |
|---|---|
| TResult | Type of elements to generate. |
Remarks
This function defers element generation until needed and streams the results.
Examples
var result = AsyncSuperEnumerable.Generate(2, n => n * n).Take(5);
The result variable, when iterated over, will yield 2, 4, 16, 256, and 65536, in turn.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Generate<TResult>(TResult, Func<TResult, CancellationToken, ValueTask<TResult>>)
Returns a sequence of values consecutively generated by a generator function.
Declaration
public static IAsyncEnumerable<TResult> Generate<TResult>(TResult initial, Func<TResult, CancellationToken, ValueTask<TResult>> generator)
Parameters
| Type | Name | Description |
|---|---|---|
| TResult | initial | Value of first element in sequence |
| Func<TResult, CancellationToken, ValueTask<TResult>> | generator | Generator function which takes the previous series element and uses it to generate the next element. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<TResult> | A sequence containing the generated values. |
Type Parameters
| Name | Description |
|---|---|
| TResult | Type of elements to generate. |
Remarks
This function defers element generation until needed and streams the results.
Examples
var result = AsyncSuperEnumerable.Generate(2, n => n * n).Take(5);
The result variable, when iterated over, will yield 2, 4, 16, 256, and 65536, in turn.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|