-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathScan.cs
More file actions
88 lines (76 loc) · 3.46 KB
/
Copy pathScan.cs
File metadata and controls
88 lines (76 loc) · 3.46 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace System.Linq
{
public static partial class EnumerableEx
{
/// <summary>
/// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <typeparam name="TAccumulate">Accumulation type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="seed">Accumulator seed value.</param>
/// <param name="accumulator">
/// Accumulation function to apply to the current accumulation value and each element of the
/// sequence.
/// </param>
/// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
public static IEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (accumulator == null)
throw new ArgumentNullException(nameof(accumulator));
return ScanCore(source, seed, accumulator);
}
/// <summary>
/// Generates a sequence of accumulated values by scanning the source sequence and applying an accumulator function.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence.</param>
/// <param name="accumulator">
/// Accumulation function to apply to the current accumulation value and each element of the
/// sequence.
/// </param>
/// <returns>Sequence with all intermediate accumulation values resulting from scanning the sequence.</returns>
public static IEnumerable<TSource> Scan<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
{
if (source == null)
throw new ArgumentNullException(nameof(source));
if (accumulator == null)
throw new ArgumentNullException(nameof(accumulator));
return ScanCore(source, accumulator);
}
private static IEnumerable<TAccumulate> ScanCore<TSource, TAccumulate>(IEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
{
var acc = seed;
yield return acc;
foreach (var item in source)
{
acc = accumulator(acc, item);
yield return acc;
}
}
private static IEnumerable<TSource> ScanCore<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
{
var hasSeed = false;
var acc = default(TSource)!;
foreach (var item in source)
{
if (!hasSeed)
{
hasSeed = true;
acc = item;
}
else
{
acc = accumulator(acc, item);
}
yield return acc;
}
}
}
}