Method PreScan
View SourcePreScan<TSource>(IAsyncEnumerable<TSource>, Func<TSource, TSource, TSource>, TSource)
Performs a pre-scan (exclusive prefix sum) on a sequence of elements.
Declaration
public static IAsyncEnumerable<TSource> PreScan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation, TSource identity)
Parameters
Type | Name | Description |
---|---|---|
IAsyncEnumerable<TSource> | source | Source sequence |
Func<TSource, TSource, TSource> | transformation | Transformation operation |
TSource | identity | Identity element (see remarks) |
Returns
Type | Description |
---|---|
IAsyncEnumerable<TSource> | The scanned sequence |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in source sequence |
Remarks
An exclusive prefix sum returns an equal-length sequence where the N-th element is the sum of the first N-1 input elements (the first element is a special case, it is set to the identity). More generally, the pre-scan allows any commutative binary operation, not just a sum.
The inclusive version of PreScan is Scan<TSource>(IAsyncEnumerable<TSource>, Func<TSource, TSource, TSource>).
This operator uses deferred execution and streams its result.
int[] values = { 1, 2, 3, 4 };
var prescan = values.PreScan((a, b) => a + b, 0);
var scan = values.Scan((a, b) => a + b);
prescan
will yield { 0, 1, 3, 6 }
, while scan
will yield { 1, 3, 6, 10 }
. This shows the relationship
between the inclusive and exclusive prefix sum.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentNullException |
|