Peforms a scan (inclusive prefix sum) on a sequence of elements.
Declaration Syntax
C# | Visual Basic | Visual C++ |
public static IEnumerable<TSource> Scan<TSource>( this IEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation )
<ExtensionAttribute> _ Public Shared Function Scan(Of TSource) ( _ source As IEnumerable(Of TSource), _ transformation As Func(Of TSource, TSource, TSource) _ ) As IEnumerable(Of TSource)
public: [ExtensionAttribute] generic<typename TSource> static IEnumerable<TSource>^ Scan( IEnumerable<TSource>^ source, Func<TSource, TSource, TSource>^ transformation )
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
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 inclusive prefix sum returns an equal-length sequence where the
N-th element is the sum of the first N input elements. More
generally, the scan allows any commutative binary operation, not
just a sum.
The exclusive version of Scan is PreScan<(Of <<'(TSource>)>>)(IEnumerable<(Of <<'(TSource>)>>), Func<(Of <<'(TSource, 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); |
Exceptions
Exception | Condition |
---|---|
InvalidOperationException | If source is empty. |
Assembly: MoreLinq (Module: MoreLinq.dll) Version: 1.0.16006.0 (1.0.16006.1845)