Skip to content

Commit

Permalink
Update documentation for ScanRight
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Nov 11, 2023
1 parent 7cad7a9 commit 9eb27ce
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 39 deletions.
13 changes: 13 additions & 0 deletions Docs/SuperLinq.Docs/apidoc/SuperLinq.SuperEnumerable.ScanRight.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
uid: ScanRight``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``0,``0})
example: [*content]
---
The following code example demonstrates how to execute a right-associative post-fix scan by key on a sequence using `ScanRight`.
[!code-csharp[](SuperLinq/ScanBy/ScanBy1.linq#L6-)]

---
uid: ScanRight``2(System.Collections.Generic.IEnumerable{``0},``1,System.Func{``0,``1,``1})
example: [*content]
---
The following code example demonstrates how to execute a right-associative post-fix scan by key on a sequence using `ScanRight`.
[!code-csharp[](SuperLinq/ScanBy/ScanBy2.linq#L6-)]
19 changes: 19 additions & 0 deletions Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight1.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Query Kind="Statements">
<NuGetReference>SuperLinq</NuGetReference>
<Namespace>SuperLinq</Namespace>
</Query>

var sequence = Enumerable.Range(1, 5)
.Select(x => x.ToString());

// execute a scan of the sequence
var result = sequence
.ScanRight((a, b) => $"({a}+{b})");

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

// This code produces the following output:
// [ "(1+(2+(3+(4+5))))", "(2+(3+(4+5)))", "(3+(4+5))", "(4+5)", "5" ]
20 changes: 20 additions & 0 deletions Docs/SuperLinq.Docs/apidoc/SuperLinq/ScanRight/ScanRight2.linq
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Query Kind="Statements">
<NuGetReference>SuperLinq</NuGetReference>
<Namespace>SuperLinq</Namespace>
</Query>

var sequence = Enumerable.Range(1, 5);

// execute a scan of the sequence
var result = sequence
.ScanRight(
"6",
(a, b) => $"({a}+{b})");

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

// This code produces the following output:
// [ "(1+(2+(3+(4+(5+6)))))", "(2+(3+(4+(5+6))))", "(3+(4+(5+6)))", "(4+(5+6))", "(5+6)", "6" ]
86 changes: 47 additions & 39 deletions Source/SuperLinq/ScanRight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Performs a right-associative scan (inclusive prefix) on a sequence of elements.
/// This operator is the right-associative version of the
/// <see cref="Scan{TSource}(IEnumerable{TSource}, Func{TSource, TSource, TSource})"/> LINQ operator.
/// Performs a right-associative scan (inclusive prefix) on a sequence of elements. This operator is the
/// right-associative version of the <see cref="Scan{TSource}(IEnumerable{TSource}, Func{TSource, TSource,
/// TSource})"/> LINQ operator.
/// </summary>
/// <typeparam name="TSource">Type of elements in source sequence.</typeparam>
/// <param name="source">Source sequence.</param>
/// <typeparam name="TSource">
/// Type of elements in source sequence.
/// </typeparam>
/// <param name="source">
/// Source sequence.
/// </param>
/// <param name="func">
/// A right-associative accumulator function to be invoked on each element.
/// Its first argument is the current value in the sequence; second argument is the previous accumulator value.
/// A right-associative accumulator function to be invoked on each element. Its first argument is the current
/// value in the sequence; second argument is the previous accumulator value.
/// </param>
/// <returns>The scanned sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <see langword="null"/>.</exception>
/// <example>
/// <code><![CDATA[
/// var result = Enumerable.Range(1, 5).Select(i => i.ToString()).ScanRight((a, b) => $"({a}+{b})");
/// ]]></code>
/// The <c>result</c> variable will contain <c>[ "(1+(2+(3+(4+5))))", "(2+(3+(4+5)))", "(3+(4+5))", "(4+5)", "5" ]</c>.
/// </example>
/// <returns>
/// The scanned sequence.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// Source sequence is consumed greedily when an iteration of the resulting sequence begins.
/// <para>
/// This method is implemented by using deferred execution. However, <paramref name="source"/> will be consumed
/// in it's entirety immediately when first element of the returned sequence is consumed.
/// </para>
/// </remarks>
public static IEnumerable<TSource> ScanRight<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
{
Expand Down Expand Up @@ -99,29 +102,34 @@ public override void CopyTo(T[] array, int arrayIndex)
}

/// <summary>
/// Performs a right-associative scan (inclusive prefix) on a sequence of elements.
/// The specified seed value is used as the initial accumulator value.
/// This operator is the right-associative version of the
/// <see cref="Scan{TSource, TState}(IEnumerable{TSource}, TState, Func{TState, TSource, TState})"/> LINQ operator.
/// Performs a right-associative scan (inclusive prefix) on a sequence of elements. This operator is the
/// right-associative version of the <see cref="Scan{TSource}(IEnumerable{TSource}, Func{TSource, TSource,
/// TSource})"/> LINQ operator.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="seed">The initial accumulator value.</param>
/// <param name="func">A right-associative accumulator function to be invoked on each element.</param>
/// <returns>The scanned sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="seed"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <see langword="null"/>.</exception>
/// <example>
/// <code><![CDATA[
/// var result = Enumerable.Range(1, 4).ScanRight("5", (a, b) => string.Format("({0}/{1})", a, b));
/// ]]></code>
/// The <c>result</c> variable will contain <c>[ "(1+(2+(3+(4+5))))", "(2+(3+(4+5)))", "(3+(4+5))", "(4+5)", "5" ]</c>.
/// </example>
/// <typeparam name="TSource">
/// Type of elements in source sequence.
/// </typeparam>
/// <param name="source">
/// Source sequence.
/// </param>
/// <param name="func">
/// A right-associative accumulator function to be invoked on each element. Its first argument is the current
/// value in the sequence; second argument is the previous accumulator value.
/// </param>
/// <param name="seed">
/// The initial accumulator value.
/// </param>
/// <returns>
/// The scanned sequence.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="func"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// Source sequence is consumed greedily when an iteration of the resulting sequence begins.
/// <para>
/// This method is implemented by using deferred execution. However, <paramref name="source"/> will be consumed
/// in it's entirety immediately when first element of the returned sequence is consumed.
/// </para>
/// </remarks>
public static IEnumerable<TAccumulate> ScanRight<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TSource, TAccumulate, TAccumulate> func)
{
Expand Down

0 comments on commit 9eb27ce

Please sign in to comment.