-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
DoWhile.cs
72 lines (65 loc) · 3.08 KB
/
DoWhile.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
namespace SuperLinq.Async;
public partial class AsyncSuperEnumerable
{
/// <summary>
/// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
/// <param name="condition">Loop condition.</param>
/// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="condition"/> is <see
/// langword="null"/>.</exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is evaluated lazily, once at the end of each loop of <paramref name="source"/>.
/// </para>
/// <para>
/// <paramref name="source"/> is cached via <see cref="Memoize{TSource}(IAsyncEnumerable{TSource})"/>, so that it
/// is only iterated once during the first loop. Successive loops will enumerate the cache instead of <paramref
/// name="source"/>.
/// </para>
/// </remarks>
public static IAsyncEnumerable<TSource> DoWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<bool> condition)
{
ArgumentNullException.ThrowIfNull(condition);
return DoWhile(source, condition.ToAsync());
}
/// <summary>
/// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
/// <param name="condition">Loop condition.</param>
/// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="condition"/> is <see
/// langword="null"/>.</exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is evaluated lazily, once at the end of each loop of <paramref name="source"/>.
/// </para>
/// <para>
/// <paramref name="source"/> is cached via <see cref="Memoize{TSource}(IAsyncEnumerable{TSource})"/>, so that it
/// is only iterated once during the first loop. Successive loops will enumerate the cache instead of <paramref
/// name="source"/>.
/// </para>
/// </remarks>
public static IAsyncEnumerable<TSource> DoWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<ValueTask<bool>> condition)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(condition);
return Core(source, condition);
static async IAsyncEnumerable<TSource> Core(
IAsyncEnumerable<TSource> source, Func<ValueTask<bool>> condition,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await using var memo = source.Memoize();
do
{
await foreach (var item in memo.WithCancellation(cancellationToken).ConfigureAwait(false))
yield return item;
}
while (await condition().ConfigureAwait(false));
}
}
}