-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEquiZip.sbntxt
129 lines (118 loc) · 4.82 KB
/
EquiZip.sbntxt
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
{{~
$arity = arity
$ordinals = ordinals
$cardinals = cardinals
~}}
namespace SuperLinq.Async;
#nullable enable
public static partial class AsyncSuperEnumerable
{
{{~ for $i in 2..4 ~}}
/// <summary>
/// <para>
/// Applies a specified function to the corresponding elements of {{ $ordinals[$i] }} sequences,
/// producing a sequence of the results.</para>
/// <para>
/// The resulting sequence has the same length as the input sequences.
/// If the input sequences are of different lengths, an exception is thrown.</para>
/// </summary>
/// <typeparam name="TResult">
/// The type of the elements of the result sequence.</typeparam>
/// <param name="resultSelector">A projection function that combines
/// elements from all of the sequences.</param>
/// <returns>A sequence of elements returned by <paramref name="resultSelector"/>.</returns>
/// <remarks>
/// This method uses deferred execution and stream its results.
/// </remarks>
/// <exception cref="global::System.ArgumentNullException"><paramref name="resultSelector"/> or any of the input sequences is null.</exception>
/// <exception cref="global::System.InvalidOperationException">
/// Any of the input sequences are shorter than the others.
/// </exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name="T{{ $cardinals[$j] }}">The type of the elements of <paramref name="{{ $ordinals[$j] }}" />.</typeparam>
/// <param name="{{ $ordinals[$j] }}">The {{ $ordinals[$j] }} sequence of elements.</param>
{{~ end ~}}
public static global::System.Collections.Generic.IAsyncEnumerable<TResult> EquiZip<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IAsyncEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult> resultSelector
)
{
{{~ for $j in 1..$i ~}}
ArgumentNullException.ThrowIfNull({{ $ordinals[$j] }});
{{~ end ~}}
ArgumentNullException.ThrowIfNull(resultSelector);
return Core(
{{~ for $j in 1..$i ~}}
{{ $ordinals[$j] }},
{{~ end ~}}
resultSelector
);
static async global::System.Collections.Generic.IAsyncEnumerable<TResult> Core(
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IAsyncEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult> resultSelector,
[EnumeratorCancellation] CancellationToken cancellationToken = default
)
{
{{~ for $j in 1..$i ~}}
await using var e{{ $j }} = {{ $ordinals[$j] }}.ConfigureAwait(false).WithCancellation(cancellationToken).GetAsyncEnumerator();
{{~ end ~}}
while (true)
{
if (!await e1.MoveNextAsync())
{
if (await e2.MoveNextAsync()
{{~
if $i >= 3
for $j in 3..$i ~}}
|| await e{{$j}}.MoveNextAsync()
{{~ end
end ~}}
)
{
ThrowHelper.ThrowInvalidOperationException("First sequence too short.");
}
yield break;
}
{{~ for $j in 2..$i ~}}
if (!await e{{$j}}.MoveNextAsync())
ThrowHelper.ThrowInvalidOperationException(
"{{ $cardinals[$j] }} sequence too short.");
{{~ end ~}}
yield return resultSelector(
{{~ for $j in 1..$i ~}}
e{{$j}}.Current{{ if !for.last }},{{ end }}
{{~ end ~}}
);
}
}
}
/// <summary>
/// Joins the corresponding elements of {{ $ordinals[$i] }} sequences,
/// producing a sequence of tuples containing them.
/// </summary>
/// <returns>A sequence of
/// <see cref="global::System.ValueTuple{ {{~ for $j in 1..$i ~}}T{{$j}}{{ if !for.last }},{{ end }}{{ end }} }" />
/// containing corresponding elements from each of the sequences.</returns>
/// <remarks>
/// This method uses deferred execution and stream its results.
/// </remarks>
/// <exception cref="global::System.ArgumentNullException">Any of the input sequences is null.</exception>
/// <exception cref="global::System.InvalidOperationException">
/// Any of the input sequences are shorter than the others.
/// </exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name="T{{ $cardinals[$j] }}">The type of the elements of <paramref name="{{ $ordinals[$j] }}" />.</typeparam>
/// <param name="{{ $ordinals[$j] }}">The {{ $ordinals[$j] }} sequence of elements.</param>
{{~ end ~}}
public static global::System.Collections.Generic.IAsyncEnumerable<({{~ for $j in 1..$i ~}}T{{ $cardinals[$j] }}{{ if !for.last }},{{ end }}{{ end }})>
EquiZip<{{~ for $j in 1..$i ~}}T{{ $cardinals[$j] }}{{ if !for.last }},{{ end }}{{ end }}>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IAsyncEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }}{{ if !for.last }},{{ end }}
{{~ end ~}}
) => EquiZip({{~ for $j in 1..$i ~}}{{ $ordinals[$j] }}, {{ end }}global::System.ValueTuple.Create);
{{~ end ~}}
}