Performs a pre-scan (exclusive prefix sum) on a sequence of elements.
Declaration Syntax
C# | Visual Basic | Visual C++ |
public static IEnumerable<TSource> PreScan<TSource>( this IEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation, TSource identity )
<ExtensionAttribute> _ Public Shared Function PreScan(Of TSource) ( _ source As IEnumerable(Of TSource), _ transformation As Func(Of TSource, TSource, TSource), _ identity As TSource _ ) As IEnumerable(Of TSource)
public: [ExtensionAttribute] generic<typename TSource> static IEnumerable<TSource>^ PreScan( IEnumerable<TSource>^ source, Func<TSource, TSource, TSource>^ transformation, TSource identity )
Generic Template Parameters
- TSource
- Type of elements in source sequence
Parameters
- source (IEnumerable<(Of <(<'TSource>)>)>)
- Source sequence
- transformation (Func<(Of <(<'TSource, TSource, TSource>)>)>)
- Transformation operation
- identity (TSource)
- Identity element (see remarks)
Return Value
The scanned sequence
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<(Of <(<'TSource>)>)>. When you use instance method syntax to call this method, omit the first parameter.
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<(Of <<'(TSource>)>>)(IEnumerable<(Of <<'(TSource>)>>), Func<(Of <<'(TSource, TSource, TSource>)>>)).
This operator uses deferred execution and streams its result.
Examples
Copy | |
---|---|
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.Zip(prescan, plus); |
Assembly: MoreLinq (Module: MoreLinq.dll) Version: 1.0.16006.0 (1.0.16006.1845)