Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add While operator #308

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions Source/SuperLinq.Async/While.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace SuperLinq.Async;

public static partial class AsyncSuperEnumerable
{
/// <summary>
/// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="condition">Loop condition.</param>
/// <param name="source">Sequence to repeat while the condition evaluates true.</param>
/// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
/// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="source"/> is <see
/// langword="null"/>.</exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is evaluated lazily, once at the start 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> While<TSource>(Func<bool> condition, IAsyncEnumerable<TSource> source)
{
return While(condition.ToAsync(), source);
}

/// <summary>
/// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
/// </summary>
/// <typeparam name="TSource">Source sequence element type.</typeparam>
/// <param name="condition">Loop condition.</param>
/// <param name="source">Sequence to repeat while the condition evaluates true.</param>
/// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
/// <exception cref="ArgumentNullException"><paramref name="condition"/> or <paramref name="source"/> is <see
/// langword="null"/>.</exception>
/// <remarks>
/// <para>
/// <paramref name="condition"/> is evaluated lazily, once at the start 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> While<TSource>(Func<ValueTask<bool>> condition, IAsyncEnumerable<TSource> source)
{
Guard.IsNotNull(condition);
Guard.IsNotNull(source);

return Core(condition, source);

static async IAsyncEnumerable<TSource> Core(
Func<ValueTask<bool>> condition,
IAsyncEnumerable<TSource> source,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await using var memo = source.Memoize();
while (await condition().ConfigureAwait(false))
{
await foreach (var item in memo.WithCancellation(cancellationToken).ConfigureAwait(false))
yield return item;
}
}
}
}
35 changes: 35 additions & 0 deletions Tests/SuperLinq.Async.Test/WhileTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Test.Async;

public class WhileTest
{
[Fact]
public void WhileIsLazy()
{
_ = AsyncSuperEnumerable.While(
BreakingFunc.Of<bool>(),
new AsyncBreakingSequence<int>());
_ = AsyncSuperEnumerable.While(
BreakingFunc.Of<ValueTask<bool>>(),
new AsyncBreakingSequence<int>());
}

[Fact]
public async Task WhileBehavior()
{
var starts = 0;
var seq = AsyncSuperEnumerable.While(
() =>
starts++ switch
{
0 or 1 => true,
2 => false,
_ => throw new TestException(),
},
AsyncEnumerable.Range(1, 10));

Assert.Equal(0, starts);
await seq.AssertSequenceEqual(
Enumerable.Range(1, 10).Concat(Enumerable.Range(1, 10)));
Assert.Equal(3, starts);
}
}