Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into x-cached-delegate
Browse files Browse the repository at this point in the history
Conflicts resolved:

- MoreLinq/AssertCount.cs
  • Loading branch information
atifaziz committed Feb 27, 2023
2 parents 2abc528 + 9b929f0 commit c325063
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 189 deletions.
16 changes: 4 additions & 12 deletions MoreLinq/AssertCount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ namespace MoreLinq

static partial class MoreEnumerable
{
#if MORELINQ

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.";

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
/// </summary>
Expand All @@ -43,7 +38,7 @@ internal static string FormatSequenceLengthErrorMessage(int cmp, int count) =>
/// </remarks>

public static IEnumerable<TSource> AssertCount<TSource>(this IEnumerable<TSource> source, int count) =>
AssertCountImpl(source, count, static (cmp, count) => new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)));
AssertCount(source, count, static (cmp, count) => new SequenceException(FormatSequenceLengthErrorMessage(cmp, count)));

/// <summary>
/// Asserts that a source sequence contains a given count of elements.
Expand All @@ -69,12 +64,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);

#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 @@ -101,5 +90,8 @@ static IEnumerable<TSource> AssertCountImpl<TSource>(IEnumerable<TSource> source
throw errorSelector(-1, 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.";
}
}
179 changes: 98 additions & 81 deletions MoreLinq/Extensions.g.cs

Large diffs are not rendered by default.

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));
}
}
}
176 changes: 96 additions & 80 deletions MoreLinq/Fold.g.cs

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions MoreLinq/Fold.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ namespace MoreLinq
/// This operator uses immediate execution and effectively buffers
/// as many items of the source sequence as necessary.
/// </remarks>
/// <typeparam name="T">Type of element in the source sequence</typeparam>
/// <typeparam name="TResult">Type of the result</typeparam>
/// <typeparam name="T">Type of element in the source sequence.</typeparam>
/// <typeparam name="TResult">Type of the result.</typeparam>
/// <param name="source">The sequence of items to fold.</param>
/// <param name="folder">Function to apply to the elements in the sequence.</param>
/// <returns>The folded value returned by <paramref name="folder"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null</exception>
/// <exception cref="ArgumentNullException"><paramref name="folder"/> is null</exception>
/// <exception cref="InvalidOperationException"><paramref name="source"/> does not contain exactly <#= e.CountString #> <#= e.ElementNoun #></exception>
/// <exception cref="ArgumentNullException">
/// Either <paramref name="source"/> or <paramref name="folder"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidOperationException"><paramref name="source"/> does not contain exactly <#= e.CountString #> <#= e.ElementNoun #>.</exception>

public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<<#= string.Join(", ", Enumerable.Repeat("T", e.Count)) #>, TResult> folder)
{
Expand Down
2 changes: 2 additions & 0 deletions MoreLinq/MoreLinq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<PackageProjectUrl>https://morelinq.github.io/</PackageProjectUrl>
<PackageLicenseFile>COPYING.txt</PackageLicenseFile>
<PackageOutputPath>..\dist</PackageOutputPath>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand All @@ -155,6 +156,7 @@

<ItemGroup>
<None Include="..\COPYING.txt" Pack="true" PackagePath="$(PackageLicenseFile)" />
<None Include="..\README.md" Pack="true" PackagePath="$(PackageReadmeFile)" />
<None Include="*.g.tt" />
<None Update="Aggregate.g.tt">
<Generator>TextTemplatingFileGenerator</Generator>
Expand Down
2 changes: 0 additions & 2 deletions MoreLinq/ToDelimitedString.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

partial class MoreEnumerable
{
Expand Down
2 changes: 0 additions & 2 deletions MoreLinq/ToDelimitedString.g.tt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ namespace MoreLinq
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

partial class MoreEnumerable
{<#
Expand Down

0 comments on commit c325063

Please sign in to comment.