-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
If.cs
82 lines (79 loc) · 2.92 KB
/
If.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
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Returns an enumerable sequence if the evaluation result of the given condition is <see langword="true"/>,
/// otherwise returns an empty sequence.
/// </summary>
/// <typeparam name="TResult">
/// Result sequence element type.
/// </typeparam>
/// <param name="condition">
/// Condition to evaluate.
/// </param>
/// <param name="thenSource">
/// Sequence to return in case the condition evaluates true.
/// </param>
/// <returns>
/// The given input sequence if the condition evaluates <see langword="true"/>; otherwise, an empty sequence.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="condition"/> or <paramref name="thenSource"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is not evaluated until enumeration. If the value is <see langword="true"/>,
/// then <paramref name="thenSource"/> is enumerated. Otherwise, the sequence will be empty.
/// </para>
/// </remarks>
public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
{
return If(condition, thenSource, []);
}
/// <summary>
/// Returns an enumerable sequence based on the evaluation result of the given condition.
/// </summary>
/// <typeparam name="TResult">
/// Result sequence element type.
/// </typeparam>
/// <param name="condition">
/// Condition to evaluate.
/// </param>
/// <param name="thenSource">
/// Sequence to return in case the condition evaluates <see langword="true"/>.
/// </param>
/// <param name="elseSource">
/// Sequence to return in case the condition evaluates <see langword="false"/>.
/// </param>
/// <returns>
/// If the condition evaluates <see langword="true"/>, then the sequence represented by <paramref
/// name="thenSource"/>; otherwise the sequence represented by <paramref name="elseSource"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="condition"/>, <paramref name="thenSource"/>, or <paramref name="elseSource"/> is <see
/// langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is not evaluated until enumeration. If the value is <see langword="true"/>,
/// then <paramref name="thenSource"/> is enumerated. Otherwise, <paramref name="elseSource"/> will be
/// enumerated.
/// </para>
/// </remarks>
public static IEnumerable<TResult> If<TResult>(
Func<bool> condition,
IEnumerable<TResult> thenSource,
IEnumerable<TResult> elseSource)
{
ArgumentNullException.ThrowIfNull(condition);
ArgumentNullException.ThrowIfNull(thenSource);
ArgumentNullException.ThrowIfNull(elseSource);
return Case(
condition,
new Dictionary<bool, IEnumerable<TResult>>
{
[true] = thenSource,
[false] = elseSource,
});
}
}