-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFold.sbntxt
49 lines (43 loc) · 1.84 KB
/
Fold.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
namespace SuperLinq.Async;
#nullable enable
public static partial class AsyncSuperEnumerable
{
{{~ for $i in 1..16 ~}}
/// <summary>
/// Returns the result of applying a function to a sequence of {{$i}} element{{if $i != 1}}s{{end}}.
/// </summary>
/// <remarks>
/// This operator uses immediate execution and buffers as many items of the source sequence as necessary.
/// </remarks>
/// <typeparam name="T">Type of element in the source sequence</typeparam>
/// <typeparam name="TResult">Type of the result</typeparam>
/// <param name="source">The sequence of items to fold.</param>
/// <param name="folder">Function to apply to the elements in the sequence.</param>
/// <returns>The folded value returned by <paramref name="folder"/>.</returns>
/// <exception cref="global::System.ArgumentNullException"><paramref name="source"/> or <paramref name="folder"/> is null.</exception>
/// <exception cref="global::System.InvalidOperationException">
/// <paramref name="source"/> does not contain exactly {{$i}} element{{if $i != 1}}s{{end}}.
/// </exception>
public static global::System.Threading.Tasks.ValueTask<TResult> Fold<T, TResult>(
this global::System.Collections.Generic.IAsyncEnumerable<T> source,
global::System.Func<{{~ for $j in 1..$i ~}}T, {{ end ~}}TResult> folder
)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(folder);
return Core(source, folder);
static async global::System.Threading.Tasks.ValueTask<TResult> Core(
global::System.Collections.Generic.IAsyncEnumerable<T> source,
global::System.Func<{{~ for $j in 1..$i ~}}T, {{ end ~}}TResult> folder
)
{
var elements = await source.AssertCount({{$i}}).ToListAsync().ConfigureAwait(false);
return folder(
{{~ for $j in 1..$i ~}}
elements[{{$j-1}}]{{ if !for.last }},{{ end }}
{{~ end ~}}
);
}
}
{{~ end ~}}
}