Click or drag to resize
MoreEnumerablePreScanTSource Method
Performs a pre-scan (exclusive prefix sum) on a sequence of elements.

Namespace:  MoreLinq
Assembly:  MoreLinq (in MoreLinq.dll) Version: 2.2.0
Syntax
public static IEnumerable<TSource> PreScan<TSource>(
	this IEnumerable<TSource> source,
	Func<TSource, TSource, TSource> transformation,
	TSource identity
)

Parameters

source
Type: System.Collections.GenericIEnumerableTSource
Source sequence
transformation
Type: SystemFuncTSource, TSource, TSource
Transformation operation
identity
Type: TSource
Identity element (see remarks)

Type Parameters

TSource
Type of elements in source sequence

Return Value

Type: IEnumerableTSource
The scanned sequence

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
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 ScanTSource(IEnumerableTSource, FuncTSource, TSource, TSource). This operator uses deferred execution and streams its result.
Examples
Func<int, int, int> plus = (a, b) => a + b;
int[] values = { 1, 2, 3, 4 };
IEnumerable<int> prescan = values.PreScan(plus, 0);
IEnumerable<int> scan = values.Scan(plus; a + b);
IEnumerable<int> result = values.ZipShortest(prescan, plus);
prescan will yield { 0, 1, 3, 6 }, while scan and result will both yield { 1, 3, 6, 10 }. This shows the relationship between the inclusive and exclusive prefix sum.
See Also