-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathZipLongest.sbntxt
111 lines (101 loc) · 4.36 KB
/
ZipLongest.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
{{~
$arity = arity
$ordinals = ordinals
$cardinals = cardinals
~}}
namespace SuperLinq.Async;
#nullable enable
public static partial class AsyncSuperEnumerable
{
{{~ for $i in 2..4 ~}}
/// <summary>
/// Returns a projection of tuples, where each tuple contains the N-th
/// element from each of the argument sequences. The resulting sequence
/// will always be as long as the longest of input sequences where the
/// default value of each of the shorter sequence element types is used
/// for padding.
/// </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 streams its results.
/// </remarks>
/// <exception cref="global::System.ArgumentNullException"><paramref name="resultSelector"/> or any of the input sequences is null.</exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name="T{{ $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> ZipLongest<{{ for $j in 1..$i }}T{{ $j }}, {{ end }}TResult>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IAsyncEnumerable<T{{ $j }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $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{{ $j }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $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();
var f{{ $j }} = true;
{{~ end ~}}
while (true)
{
{{~ for $j in 1..$i ~}}
var v{{ $j }} = (f{{ $j }} && (f{{ $j }} = await e{{ $j }}.MoveNextAsync()))
? e{{ $j }}.Current : default(T{{ $j }});
{{~ end ~}}
if (!f1{{ for $j in 2..$i }} && !f{{ $j }}{{ end }})
yield break;
yield return resultSelector(
{{~ for $j in 1..$i ~}}
v{{$j}}{{ if !for.last }},{{ end }}
{{~ end ~}}
);
}
}
}
/// <summary>
/// Returns a projection of tuples, where each tuple contains the N-th
/// element from each of the argument sequences. The resulting sequence
/// will always be as long as the longest of input sequences where the
/// default value of each of the shorter sequence element types is used
/// for padding.
/// </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 streams its results.
/// </remarks>
/// <exception cref="global::System.ArgumentNullException">Any of the input sequences is null.</exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name="T{{ $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{{ $j }}?{{ if !for.last }},{{ end }}{{ end }})>
ZipLongest<{{~ for $j in 1..$i ~}}T{{ $j }}{{ if !for.last }},{{ end }}{{ end }}>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IAsyncEnumerable<T{{ $j }}> {{ $ordinals[$j] }}{{ if !for.last }},{{ end }}
{{~ end ~}}
) => ZipLongest({{~ for $j in 1..$i ~}}{{ $ordinals[$j] }}, {{ end }}global::System.ValueTuple.Create);
{{~ end ~}}
}