Method ScanBy
| Edit this page View SourceScanBy<TSource, TKey, TState>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, TState>, Func<TState, TKey, TSource, TState>)
Applies an accumulator function over sequence element keys, returning the keys along with intermediate accumulator states.
Declaration
public static IEnumerable<(TKey key, TState state)> ScanBy<TSource, TKey, TState>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TState> seedSelector, Func<TState, TKey, TSource, TState> accumulator)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, TKey> | keySelector | A function that returns the key given an element. |
Func<TKey, TState> | seedSelector | A function to determine the initial value for the accumulator that is invoked once per key encountered. |
Func<TState, TKey, TSource, TState> | accumulator | An accumulator function invoked for each element. |
Returns
Type | Description |
---|---|
IEnumerable<(TKey key, TState state)> | A sequence of keys paired with intermediate accumulator states. |
Type Parameters
Name | Description |
---|---|
TSource | Type of the elements of the source sequence. |
TKey | The type of the key. |
TState | Type of the state. |
Remarks
This operator uses deferred execution and streams its result.
Examples
The following code example demonstrates how to execute a post-fix scan by key on a sequence using ScanBy
.
var sequence = Enumerable.Range(1, 10);
// execute a scan of the sequence
var result = sequence
.ScanBy(
x => x % 2,
k => k * 1000,
(a, k, b) => a + b);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(1, 1001), (0, 2), (1, 1004), (0, 6), (1, 1009), (0, 12), (1, 1016), (0, 20), (1, 1025), (0, 30)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ScanBy<TSource, TKey, TState>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TKey, TState>, Func<TState, TKey, TSource, TState>, IEqualityComparer<TKey>?)
Applies an accumulator function over sequence element keys, returning the keys along with intermediate accumulator states.
Declaration
public static IEnumerable<(TKey key, TState state)> ScanBy<TSource, TKey, TState>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TKey, TState> seedSelector, Func<TState, TKey, TSource, TState> accumulator, IEqualityComparer<TKey>? comparer)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Func<TSource, TKey> | keySelector | A function that returns the key given an element. |
Func<TKey, TState> | seedSelector | A function to determine the initial value for the accumulator that is invoked once per key encountered. |
Func<TState, TKey, TSource, TState> | accumulator | An accumulator function invoked for each element. |
IEqualityComparer<TKey> | comparer | The equality comparer to use to determine whether or not keys are equal. If null, the
default equality comparer for |
Returns
Type | Description |
---|---|
IEnumerable<(TKey key, TState state)> | A sequence of keys paired with intermediate accumulator states. |
Type Parameters
Name | Description |
---|---|
TSource | Type of the elements of the source sequence. |
TKey | The type of the key. |
TState | Type of the state. |
Remarks
This operator uses deferred execution and streams its result.
Examples
The following code example demonstrates how to execute a post-fix scan by key on a sequence using ScanBy
.
var sequence = new[]
{
"BAR",
"foo",
"Baz",
"Qux",
"BAZ",
"FOO",
"bAr",
"baz",
"fOo",
"BaZ",
};
// execute a scan of the sequence
var result = sequence
.ScanBy(
x => x[..1],
k => 0,
(a, k, b) => a + 1,
StringComparer.OrdinalIgnoreCase);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [(B, 1), (f, 1), (B, 2), (Q, 1), (B, 3), (F, 2), (b, 4), (b, 5), (f, 3), (B, 6)]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|