Skip to content
Closed
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
15 changes: 14 additions & 1 deletion MoreLinq.Test/SkipLastTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void SkipLastWithCountLesserThanOne(int skip)
}

[Test]
public void SkipLast()
public void SkipLastEnumerable()
{
const int take = 100;
const int skip = 20;
Expand All @@ -44,6 +44,19 @@ public void SkipLast()
Assert.That(expectations, Is.EqualTo(sequence.SkipLast(skip)));
}

[Test]
public void SkipLastCollection()
{
const int take = 100;
const int skip = 20;

var sequence = Enumerable.Range(1, take).ToArray();

var expectations = sequence.Take(take - skip);

Assert.That(expectations, Is.EqualTo(sequence.SkipLast(skip)));
}

[TestCase(5)]
[TestCase(6)]
public void SkipLastWithSequenceShorterThanCount(int skip)
Expand Down
28 changes: 22 additions & 6 deletions MoreLinq/SkipLast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,28 @@ public static IEnumerable<T> SkipLast<T>(this IEnumerable<T> source, int count)
if (count < 1)
return source;

return
source.TryGetCollectionCount() is int collectionCount
? source.Take(collectionCount - count)
: source.CountDown(count, (e, cd) => (Element: e, Countdown: cd ))
.TakeWhile(e => e.Countdown == null)
.Select(e => e.Element);
var collectionCount = source.TryGetCollectionCount();
if (collectionCount.HasValue)
{
return source.Take(collectionCount.Value - count);
}

return _(); IEnumerable<T> _()
{
var queue = new Queue<T>(count);
using var enumerator = source.GetEnumerator();

while (count-- > 0 && enumerator.MoveNext())
{
queue.Enqueue(enumerator.Current);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MoveNext is called after the sequence end if it is too short
Tests should have detected that.

while (enumerator.MoveNext())
{
yield return queue.Dequeue();
queue.Enqueue(enumerator.Current);
}
}
}
}
}