-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Case.cs
109 lines (104 loc) · 4.02 KB
/
Case.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
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Returns a sequence from a dictionary based on the result of evaluating a selector function.
/// </summary>
/// <typeparam name="TValue">
/// Type of the selector value.
/// </typeparam>
/// <typeparam name="TResult">
/// Result sequence element type.
/// </typeparam>
/// <param name="selector">
/// Selector function used to pick a sequence from the given sources.
/// </param>
/// <param name="sources">
/// Dictionary mapping selector values onto resulting sequences.
/// </param>
/// <returns>
/// The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="selector"/> or <paramref name="sources"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentNullException">
/// (Thrown lazily) The sequence in <paramref name="sources"/> selected by the result of <paramref
/// name="selector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// <paramref name="selector"/> is not evaluated until enumeration. The value returned will be used to select a
/// sequence from <paramref name="sources"/>; enumeration will continue with items from that sequence.
/// </para>
/// <para>
/// If the value returned by <paramref name="selector"/> is not present in <paramref name="sources"/>, the
/// resulting sequence will be empty.
/// </para>
/// </remarks>
public static IEnumerable<TResult> Case<TValue, TResult>(
Func<TValue> selector,
IDictionary<TValue, IEnumerable<TResult>> sources)
where TValue : notnull
{
return Case(selector, sources, []);
}
/// <summary>
/// Returns a sequence from a dictionary based on the result of evaluating a selector function.
/// </summary>
/// <typeparam name="TValue">
/// Type of the selector value.
/// </typeparam>
/// <typeparam name="TResult">
/// Result sequence element type.
/// </typeparam>
/// <param name="selector">
/// Selector function used to pick a sequence from the given sources.
/// </param>
/// <param name="sources">
/// Dictionary mapping selector values onto resulting sequences.
/// </param>
/// <param name="defaultSource">
/// Default sequence to return in case there's no corresponding source for the computed selector value.
/// </param>
/// <returns>
/// The source sequence corresponding with the evaluated selector value; otherwise, the <paramref
/// name="defaultSource"/> sequence.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="selector"/>, <paramref name="sources"/> or <paramref name="defaultSource"/> is <see
/// langword="null"/>.
/// </exception>
/// <exception cref="ArgumentNullException">
/// (Thrown lazily) The sequence in <paramref name="sources"/> selected by the result of <paramref
/// name="selector"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// <paramref name="selector"/> is not evaluated until enumeration. The value returned will be used to select a
/// sequence from <paramref name="sources"/>; enumeration will continue with items from that sequence.
/// </para>
/// </remarks>
public static IEnumerable<TResult> Case<TValue, TResult>(
Func<TValue> selector,
IDictionary<TValue, IEnumerable<TResult>> sources,
IEnumerable<TResult> defaultSource)
where TValue : notnull
{
ArgumentNullException.ThrowIfNull(selector);
ArgumentNullException.ThrowIfNull(sources);
ArgumentNullException.ThrowIfNull(defaultSource);
return Core(selector, sources, defaultSource);
static IEnumerable<TResult> Core(
Func<TValue> selector,
IDictionary<TValue, IEnumerable<TResult>> sources,
IEnumerable<TResult> defaultSource)
{
if (!sources.TryGetValue(selector(), out var source))
source = defaultSource;
ArgumentNullException.ThrowIfNull(source);
foreach (var el in source)
yield return el;
}
}
}