-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Scan.cs
112 lines (102 loc) · 3.16 KB
/
Scan.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Performs a scan (inclusive prefix sum) on a sequence of elements. This operator is similar to <see
/// cref="Enumerable.Aggregate{TSource}"/> except that <see cref="Scan{TSource}"/> returns the sequence of
/// intermediate results as well as the final one.
/// </summary>
/// <typeparam name="TSource">
/// Type of elements in source sequence
/// </typeparam>
/// <param name="source">
/// Source sequence
/// </param>
/// <param name="transformation">
/// Transformation operation
/// </param>
/// <returns>
/// The scanned sequence
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="transformation"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This operator uses deferred execution and streams its result.
/// </para>
/// </remarks>
public static IEnumerable<TSource> Scan<TSource>(
this IEnumerable<TSource> source,
Func<TSource, TSource, TSource> transformation)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(transformation);
return Core(source, transformation);
static IEnumerable<TSource> Core(IEnumerable<TSource> source, Func<TSource, TSource, TSource> transformation)
{
using var e = source.GetEnumerator();
if (!e.MoveNext())
yield break;
var state = e.Current;
yield return state;
while (e.MoveNext())
{
state = transformation(state, e.Current);
yield return state;
}
}
}
/// <summary>
/// Performs a scan (inclusive prefix sum) on a sequence of elements. This operator is similar to <see
/// cref="Enumerable.Aggregate{TSource,TState}"/> except that <see cref="Scan{TSource,TState}"/> returns the sequence of
/// intermediate results as well as the final one.
/// </summary>
/// <typeparam name="TSource">
/// Type of elements in source sequence
/// </typeparam>
/// <typeparam name="TState">
/// Type of state
/// </typeparam>
/// <param name="source">
/// Source sequence
/// </param>
/// <param name="seed">
/// Initial state to seed
/// </param>
/// <param name="transformation">
/// Transformation operation
/// </param>
/// <returns>
/// The scanned sequence
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="transformation"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// This operator uses deferred execution and streams its result.
/// </para>
/// </remarks>
public static IEnumerable<TState> Scan<TSource, TState>(
this IEnumerable<TSource> source,
TState seed,
Func<TState, TSource, TState> transformation)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(transformation);
return Core(source, seed, transformation);
static IEnumerable<TState> Core(
IEnumerable<TSource> source,
TState state,
Func<TState, TSource, TState> transformation)
{
yield return state;
foreach (var e in source)
{
state = transformation(state, e);
yield return state;
}
}
}
}