Method ScanRight
| Edit this pageScanRight<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>)
Performs a right-associative scan (inclusive prefix) on a sequence of elements. This operator is the right-associative version of the Scan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) LINQ operator.
Declaration
public static IEnumerable<TSource> ScanRight<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| Func<TSource, TSource, TSource> | func | A right-associative accumulator function to be invoked on each element. Its first argument is the current value in the sequence; second argument is the previous accumulator value. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TSource> | The scanned sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Type of elements in source sequence. |
Remarks
This method is implemented by using deferred execution. However, source will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to execute a right-associative post-fix scan on a sequence using ScanRight.
var sequence = Enumerable.Range(1, 5)
.Select(x => x.ToString());
// execute a scan of the sequence
var result = sequence
.ScanRight((a, b) => $"({a}+{b})");
Console.WriteLine(
"[ \"" +
string.Join("\", \"", result) +
"\" ]");
// This code produces the following output:
// [ "(1+(2+(3+(4+5))))", "(2+(3+(4+5)))", "(3+(4+5))", "(4+5)", "5" ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
ScanRight<TSource, TAccumulate>(IEnumerable<TSource>, TAccumulate, Func<TSource, TAccumulate, TAccumulate>)
Performs a right-associative scan (inclusive prefix) on a sequence of elements. This operator is the right-associative version of the Scan<TSource>(IEnumerable<TSource>, Func<TSource, TSource, TSource>) LINQ operator.
Declaration
public static IEnumerable<TAccumulate> ScanRight<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TSource, TAccumulate, TAccumulate> func)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| TAccumulate | seed | The initial accumulator value. |
| Func<TSource, TAccumulate, TAccumulate> | func | A right-associative accumulator function to be invoked on each element. Its first argument is the current value in the sequence; second argument is the previous accumulator value. |
Returns
| Type | Description |
|---|---|
| IEnumerable<TAccumulate> | The scanned sequence. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Type of elements in source sequence. |
| TAccumulate | Type of the state accumulator. |
Remarks
This method is implemented by using deferred execution. However, source will be consumed
in it's entirety immediately when first element of the returned sequence is consumed.
Examples
The following code example demonstrates how to execute a right-associative post-fix scan on a sequence using ScanRight.
var sequence = Enumerable.Range(1, 5);
// execute a scan of the sequence
var result = sequence
.ScanRight(
"6",
(a, b) => $"({a}+{b})");
Console.WriteLine(
"[ \"" +
string.Join("\", \"", result) +
"\" ]");
// This code produces the following output:
// [ "(1+(2+(3+(4+(5+6)))))", "(2+(3+(4+(5+6))))", "(3+(4+(5+6)))", "(4+(5+6))", "(5+6)", "6" ]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|