Skip to content
Closed
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
16 changes: 14 additions & 2 deletions MoreLinq/Slice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Linq;

public static partial class MoreEnumerable
{
Expand Down Expand Up @@ -57,9 +56,22 @@ public static IEnumerable<T> Slice<T>(this IEnumerable<T> sequence, int startInd
{
IList<T> list => SliceList(list.Count, i => list[i]),
IReadOnlyList<T> list => SliceList(list.Count, i => list[i]),
var seq => seq.Skip(startIndex).Take(count)
_ => SliceDefault()
};

IEnumerable<T> SliceDefault()
{
var lastIndex = startIndex + count;
var index = -1;
foreach (var item in sequence)
{
if (++index >= lastIndex)
yield break;
else if (index >= startIndex)
yield return item;
}
}

IEnumerable<T> SliceList(int listCount, Func<int, T> indexer)
{
var countdown = count;
Expand Down