From 9c17aae35a5d8194776bc348028e78b36c023d6b Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Tue, 24 Jan 2023 08:41:45 +0100 Subject: [PATCH] Improve optimisation for a list source in "CountDown" --- MoreLinq/CountDown.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/MoreLinq/CountDown.cs b/MoreLinq/CountDown.cs index a46186a89..1f5f72dad 100644 --- a/MoreLinq/CountDown.cs +++ b/MoreLinq/CountDown.cs @@ -65,15 +65,11 @@ public static IEnumerable CountDown(this IEnumerable sou IEnumerable IterateList(IListLike 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 IterateCollection(int i)