Skip to content

Commit

Permalink
Update documentation for Generate
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Sep 6, 2023
1 parent ffa07bc commit 0f18124
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
uid: SuperLinq.SuperEnumerable.Generate``1(``0,System.Func{``0,``0})
example: [*content]
---
The following code example demonstrates how to generate a sequence from a generator function using `Generate`.
[!code-csharp[](SuperLinq/Generate/Generate.linq#L6-)]
17 changes: 17 additions & 0 deletions Docs/SuperLinq.Docs/apidoc/SuperLinq/Generate/Generate.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Query Kind="Statements">
<NuGetReference>SuperLinq</NuGetReference>
<Namespace>SuperLinq</Namespace>
</Query>

// Generate a sequence using a generator function
var result = SuperEnumerable
.Generate(1, n => n * 2)
.Take(10);

Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");

// This code produces the following output:
// [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
30 changes: 16 additions & 14 deletions Source/SuperLinq/Generate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
public static partial class SuperEnumerable
{
/// <summary>
/// Returns a sequence of values consecutively generated by a generator function.
/// Returns a sequence of values consecutively generated by a generator function.
/// </summary>
/// <typeparam name="TResult">Type of elements to generate.</typeparam>
/// <param name="initial">Value of first element in sequence</param>
/// <typeparam name="TResult">
/// Type of elements to generate.
/// </typeparam>
/// <param name="initial">
/// Value of first element in sequence
/// </param>
/// <param name="generator">
/// Generator function which takes the previous series element and uses it to generate the next element.
/// Generator function which takes the previous series element and uses it to generate the next element.
/// </param>
/// <returns>A sequence containing the generated values.</returns>
/// <exception cref="ArgumentNullException"><paramref name="generator"/> is <see langword="null"/>.</exception>
/// <returns>
/// A sequence containing the generated values.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="generator"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This function defers element generation until needed and streams the results.
/// This function defers element generation until needed and streams the results. It is treated as an
/// infinite sequence, and will not terminate.
/// </remarks>
/// <example>
/// <code><![CDATA[
/// var result = SuperEnumerable.Generate(2, n => n * n).Take(5);
/// ]]></code>
/// The <c>result</c> variable, when iterated over, will yield 2, 4, 16, 256, and 65536, in turn.
/// </example>

public static IEnumerable<TResult> Generate<TResult>(TResult initial, Func<TResult, TResult> generator)
{
Guard.IsNotNull(generator);
Expand Down

0 comments on commit 0f18124

Please sign in to comment.