MoreEnumerableScanTSource(IEnumerableTSource, FuncTSource, TSource, TSource) Method
Performs a scan (inclusive prefix sum) on a sequence of elements.
Namespace: MoreLinqAssembly: MoreLinq (in MoreLinq.dll) Version: 3.4.0+b99a6a8cc504caf2d48372fe54a2f8116c59cd0c
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
)
[<ExtensionAttribute>]
static member Scan :
source : IEnumerable<'TSource> *
transformation : Func<'TSource, 'TSource, 'TSource> -> IEnumerable<'TSource>
- source IEnumerableTSource
- Source sequence
- transformation FuncTSource, TSource, TSource
- Transformation operation
- TSource
- Type of elements in source sequence
IEnumerableTSourceThe scanned sequenceIn 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).
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
PreScanTSource(IEnumerableTSource, FuncTSource, 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);
var result = values.EquiZip(scan, ValueTuple.Create);
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.