Skip to content

Commit bdefb3c

Browse files
Better fix for IAsyncEnumerable.SkipLast, match algorithm of IEnumerable.SkipLast
1 parent 4e1f030 commit bdefb3c

File tree

2 files changed

+8
-20
lines changed
  • Ix.NET/Source

2 files changed

+8
-20
lines changed

Ix.NET/Source/System.Linq.Async.Tests/System/Linq/Operators/SkipLast.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public async Task SkipLast_Zero_NoAlias()
7070
var ys = xs.SkipLast(0);
7171

7272
Assert.NotSame(xs, ys);
73-
await NoNextAsync(ys.GetAsyncEnumerator());
73+
var e = ys.GetAsyncEnumerator();
74+
await HasNextAsync(e, 1);
7475
}
7576

7677
private async IAsyncEnumerable<int> Xs()

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/SkipLast.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,14 @@ public static IAsyncEnumerable<TSource> SkipLast<TSource>(this IAsyncEnumerable<
4545

4646
static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, int count, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
4747
{
48-
var queue = new Queue<TSource>();
48+
var q = new Queue<TSource>();
4949

50-
await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
51-
52-
while (await e.MoveNextAsync())
50+
await foreach (var x in source.WithCancellation(cancellationToken))
5351
{
54-
if (queue.Count == count)
55-
{
56-
do
57-
{
58-
if (queue.Count > 0)
59-
yield return queue.Dequeue();
60-
queue.Enqueue(e.Current);
61-
}
62-
while (await e.MoveNextAsync());
63-
break;
64-
}
65-
else
66-
{
67-
queue.Enqueue(e.Current);
68-
}
52+
q.Enqueue(x);
53+
54+
if (q.Count > count)
55+
yield return q.Dequeue();
6956
}
7057
}
7158
}

0 commit comments

Comments
 (0)