Skip to content

Commit

Permalink
Improve optimisation for a list source in "CountDown"
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored Jan 24, 2023
1 parent d587f73 commit 9c17aae
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions MoreLinq/CountDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,11 @@ public static IEnumerable<TResult> CountDown<T, TResult>(this IEnumerable<T> sou

IEnumerable<TResult> IterateList(IListLike<T> list)
{
var countdown = Math.Min(count, list.Count);
var listCount = list.Count;
var countdown = Math.Min(count, listCount);

for (var i = 0; i < list.Count; i++)
{
var cd = list.Count - i <= count
? --countdown
: (int?) null;
yield return resultSelector(list[i], cd);
}
for (var i = 0; i < listCount; i++)
yield return resultSelector(list[i], listCount - i <= count ? --countdown : null);
}

IEnumerable<TResult> IterateCollection(int i)
Expand Down

0 comments on commit 9c17aae

Please sign in to comment.