Method Scan
| Edit this page View SourceScan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)
Performs a scan (inclusive prefix sum) on a sequence of elements. This operator is similar to Aggregate<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) except that Scan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) returns the sequence of intermediate results as well as the final one.
Declaration
public static IEnumerable<TSource> Scan<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence |
Func<TSource, TSource, TSource> | transformation | Transformation operation |
Returns
Type | Description |
---|---|
IEnumerable<TSource> | The scanned sequence |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in source sequence |
Remarks
This operator uses deferred execution and streams its result.
Examples
The following code example demonstrates how to execute a post-fix scan on a sequence using Scan
.
var sequence = Enumerable.Range(1, 4);
// execute a scan of the sequence
var result = sequence.Scan((a, b) => a + b);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 3, 6, 10]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Scan<TSource, TState>(IEnumerable<TSource>, TState, Func<TState, TSource, TState>)
Performs a scan (inclusive prefix sum) on a sequence of elements. This operator is similar to Aggregate<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TAccumulate, TSource, TAccumulate>) except that Scan<TSource, TState>(IEnumerable<TSource>, TState, Func<TState, TSource, TState>) returns the sequence of intermediate results as well as the final one.
Declaration
public static IEnumerable<TState> Scan<TSource, TState>(this IEnumerable<TSource> source, TState seed, Func<TState, TSource, TState> transformation)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | Source sequence |
TState | seed | Initial state to seed |
Func<TState, TSource, TState> | transformation | Transformation operation |
Returns
Type | Description |
---|---|
IEnumerable<TState> | The scanned sequence |
Type Parameters
Name | Description |
---|---|
TSource | Type of elements in source sequence |
TState | Type of state |
Remarks
This operator uses deferred execution and streams its result.
Examples
The following code example demonstrates how to execute a post-fix scan on a sequence using Scan
.
var sequence = Enumerable.Range(1, 4);
// execute a scan of the sequence
var result = sequence
.Scan(
"0",
(a, b) => $"({a} + {b})");
Console.WriteLine(
"[ \"" +
string.Join("\", \"", result) +
"\" ]");
// This code produces the following output:
// [ "0", "(0 + 1)", "((0 + 1) + 2)", "(((0 + 1) + 2) + 3)", "((((0 + 1) + 2) + 3) + 4)" ]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|