Skip to content

Commit

Permalink
Implement "Fold" entirely in its own terms
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Feb 26, 2023
1 parent 777aa24 commit 14f427a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
22 changes: 7 additions & 15 deletions MoreLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ namespace MoreLinq

static partial class MoreEnumerable
{
#if MORELINQ

static readonly Func<int, int, Exception> DefaultErrorSelector = OnAssertCountFailure;

/// <summary>
Expand All @@ -42,7 +40,7 @@ static partial class MoreEnumerable
/// </remarks>

public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource> source, int count) =>
AssertCountImpl(source, count, DefaultErrorSelector);
AssertCount(source, count, DefaultErrorSelector);

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
Expand All @@ -68,18 +66,6 @@ public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource
/// </remarks>

public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource> source,
int count, Func<int, int, Exception> 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<TSource> AssertCountImpl<TSource>(IEnumerable<TSource> source,
int count, Func<int, int, Exception> errorSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
Expand All @@ -106,5 +92,11 @@ static IEnumerable<TSource> AssertCountImpl<TSource>(IEnumerable<TSource> 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.";
}
}
19 changes: 12 additions & 7 deletions MoreLinq/Fold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ static partial class MoreEnumerable
static T[] Fold<T>(this IEnumerable<T> 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<int, int, Exception> 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));
}
}
}

0 comments on commit 14f427a

Please sign in to comment.