Skip to content

Commit 05180a9

Browse files
Update documentation for PreScan
1 parent 1a62704 commit 05180a9

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
uid: SuperLinq.SuperEnumerable.PreScan``1(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``0,``0},``0)
3+
example: [*content]
4+
---
5+
The following code example demonstrates how to perform an exclusive pre-fix scan on a sequence using `PreScan`.
6+
[!code-csharp[](SuperLinq/PreScan/PreScan1.linq#L6-)]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Query Kind="Statements">
2+
<NuGetReference>SuperLinq</NuGetReference>
3+
<Namespace>SuperLinq</Namespace>
4+
</Query>
5+
6+
var sequence = Enumerable.Range(1, 4);
7+
8+
// execute a scan of the sequence, returning the aggregation before processing the element
9+
var result = sequence.PreScan((a, b) => a + b, 0);
10+
11+
Console.WriteLine(
12+
"[" +
13+
string.Join(", ", result) +
14+
"]");
15+
16+
// This code produces the following output:
17+
// [0, 1, 3, 6]

Source/SuperLinq/PreScan.cs

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,38 @@ namespace SuperLinq;
55
public static partial class SuperEnumerable
66
{
77
/// <summary>
8-
/// Performs a pre-scan (exclusive prefix sum) on a sequence of elements.
8+
/// Performs a pre-scan (exclusive prefix sum) on a sequence of elements.
99
/// </summary>
10+
/// <typeparam name="TSource">
11+
/// Type of elements in source sequence
12+
/// </typeparam>
13+
/// <param name="source">
14+
/// Source sequence
15+
/// </param>
16+
/// <param name="transformation">
17+
/// An accumulator function to be invoked on each element.
18+
/// </param>
19+
/// <param name="identity">
20+
/// The initial accumulator value.
21+
/// </param>
22+
/// <exception cref="ArgumentNullException">
23+
/// <paramref name="source"/> or <paramref name="transformation"/> is <see langword="null"/>.
24+
/// </exception>
25+
/// <returns>
26+
/// The scanned sequence
27+
/// </returns>
1028
/// <remarks>
1129
/// <para>
12-
/// An exclusive prefix sum returns an equal-length sequence where the
13-
/// N-th element is the sum of the first N-1 input elements (the first
14-
/// element is a special case, it is set to the identity). More
15-
/// generally, the pre-scan allows any commutative binary operation,
16-
/// not just a sum.
30+
/// An exclusive prefix scan returns an equal-length sequence where the N-th element is the aggregation of the
31+
/// first N-1 input elements, where the first element is simply the <paramref name="identity"/> value.
1732
/// </para>
1833
/// <para>
19-
/// The inclusive version of PreScan is <see cref="ScanEx{TSource}"/>.
34+
/// The inclusive version of PreScan is <see cref="Scan{TSource}"/>.
2035
/// </para>
2136
/// <para>
22-
/// This operator uses deferred execution and streams its result.
37+
/// This operator uses deferred execution and streams its result.
2338
/// </para>
24-
/// <example>
25-
/// <code><![CDATA[
26-
/// int[] values = { 1, 2, 3, 4 };
27-
/// var prescan = values.PreScan((a, b) => a + b, 0);
28-
/// var scan = values.Scan((a, b) => a + b);
29-
/// ]]></code>
30-
/// <c>prescan</c> will yield <c>{ 0, 1, 3, 6 }</c>, while <c>scan</c>
31-
/// will yield <c>{ 1, 3, 6, 10 }</c>. This shows the relationship
32-
/// between the inclusive and exclusive prefix sum.
33-
/// </example>
3439
/// </remarks>
35-
/// <typeparam name="TSource">Type of elements in source sequence</typeparam>
36-
/// <param name="source">Source sequence</param>
37-
/// <param name="transformation">Transformation operation</param>
38-
/// <param name="identity">Identity element (see remarks)</param>
39-
/// <returns>The scanned sequence</returns>
40-
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null</exception>
41-
/// <exception cref="ArgumentNullException"><paramref name="transformation"/> is null</exception>
4240
public static IEnumerable<TSource> PreScan<TSource>(
4341
this IEnumerable<TSource> source,
4442
Func<TSource, TSource, TSource> transformation,

0 commit comments

Comments
 (0)