Skip to content

Commit 9c17aae

Browse files
authored
Improve optimisation for a list source in "CountDown"
1 parent d587f73 commit 9c17aae

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

MoreLinq/CountDown.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,11 @@ public static IEnumerable<TResult> CountDown<T, TResult>(this IEnumerable<T> sou
6565

6666
IEnumerable<TResult> IterateList(IListLike<T> list)
6767
{
68-
var countdown = Math.Min(count, list.Count);
68+
var listCount = list.Count;
69+
var countdown = Math.Min(count, listCount);
6970

70-
for (var i = 0; i < list.Count; i++)
71-
{
72-
var cd = list.Count - i <= count
73-
? --countdown
74-
: (int?) null;
75-
yield return resultSelector(list[i], cd);
76-
}
71+
for (var i = 0; i < listCount; i++)
72+
yield return resultSelector(list[i], listCount - i <= count ? --countdown : null);
7773
}
7874

7975
IEnumerable<TResult> IterateCollection(int i)

0 commit comments

Comments
 (0)