diff --git a/MoreLinq/AssertCount.cs b/MoreLinq/AssertCount.cs index 0273bf060..debe5f3ae 100644 --- a/MoreLinq/AssertCount.cs +++ b/MoreLinq/AssertCount.cs @@ -22,8 +22,6 @@ namespace MoreLinq static partial class MoreEnumerable { -#if MORELINQ - static readonly Func DefaultErrorSelector = OnAssertCountFailure; /// @@ -42,7 +40,7 @@ static partial class MoreEnumerable /// public static IEnumerable AssertCount(this IEnumerable source, int count) => - AssertCountImpl(source, count, DefaultErrorSelector); + AssertCount(source, count, DefaultErrorSelector); /// /// Asserts that a source sequence contains a given count of elements. @@ -68,18 +66,6 @@ public static IEnumerable AssertCount(this IEnumerable public static IEnumerable AssertCount(this IEnumerable source, - int count, Func errorSelector) => - AssertCountImpl(source, count, errorSelector); - - static Exception OnAssertCountFailure(int cmp, int count) => - new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)); - - internal static string FormatSequenceLengthErrorMessage(int cmp, int count) => - $"Sequence contains too {(cmp < 0 ? "few" : "many")} elements when exactly {count:N0} {(count == 1 ? "was" : "were")} expected."; - -#endif - - static IEnumerable AssertCountImpl(IEnumerable source, int count, Func errorSelector) { if (source == null) throw new ArgumentNullException(nameof(source)); @@ -106,5 +92,11 @@ static IEnumerable AssertCountImpl(IEnumerable source throw errorSelector(-1, count); } } + + static Exception OnAssertCountFailure(int cmp, int count) => + new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)); + + internal static string FormatSequenceLengthErrorMessage(int cmp, int count) => + $"Sequence contains too {(cmp < 0 ? "few" : "many")} elements when exactly {count:N0} {(count == 1 ? "was" : "were")} expected."; } } diff --git a/MoreLinq/Extensions.g.cs b/MoreLinq/Extensions.g.cs index 4ec625245..a19f82c10 100644 --- a/MoreLinq/Extensions.g.cs +++ b/MoreLinq/Extensions.g.cs @@ -540,7 +540,8 @@ public static partial class AssertCountExtension /// public static IEnumerable AssertCount(this IEnumerable source, - int count, Func errorSelector) => MoreEnumerable.AssertCount(source, count, errorSelector); + int count, Func errorSelector) + => MoreEnumerable.AssertCount(source, count, errorSelector); } diff --git a/MoreLinq/Fold.cs b/MoreLinq/Fold.cs index fde8568e5..f7ad9d094 100644 --- a/MoreLinq/Fold.cs +++ b/MoreLinq/Fold.cs @@ -25,15 +25,20 @@ static partial class MoreEnumerable static T[] Fold(this IEnumerable source, int count) { var elements = new T[count]; - foreach (var e in AssertCountImpl(source.Index(), count, OnFolderSourceSizeErrorSelector)) - elements[e.Key] = e.Value; + var i = 0; - return elements; - } + foreach (var item in source) + { + elements[i] = i < count ? item : throw LengthError(1); + i++; + } - static readonly Func OnFolderSourceSizeErrorSelector = OnFolderSourceSizeError; + if (i < count) + throw LengthError(-1); - static Exception OnFolderSourceSizeError(int cmp, int count) => - new InvalidOperationException(FormatSequenceLengthErrorMessage(cmp, count)); + return elements; + + InvalidOperationException LengthError(int cmp) => new(FormatSequenceLengthErrorMessage(cmp, count)); + } } }