Skip to content

Commit

Permalink
Hide "CountBy" in .NET 9+
Browse files Browse the repository at this point in the history
This is squashed merge of PR #1092 that closes #1020.
  • Loading branch information
atifaziz authored Dec 28, 2024
1 parent 90a35c5 commit 845294d
Show file tree
Hide file tree
Showing 8 changed files with 756 additions and 15 deletions.
17 changes: 9 additions & 8 deletions MoreLinq.Test/CountByTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CountByTest
[Test]
public void CountBySimpleTest()
{
var result = new[] { 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2 }.CountBy(c => c);
var result = MoreEnumerable.CountBy([1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 1, 2 ], c => c);

result.AssertSequenceEqual(
KeyValuePair.Create(1, 4),
Expand All @@ -40,7 +40,7 @@ public void CountBySimpleTest()
[Test]
public void CountByWithSecondOccurenceImmediatelyAfterFirst()
{
var result = "jaffer".CountBy(c => c);
var result = MoreEnumerable.CountBy("jaffer", c => c);

result.AssertSequenceEqual(
KeyValuePair.Create('j', 1),
Expand All @@ -53,7 +53,7 @@ public void CountByWithSecondOccurenceImmediatelyAfterFirst()
[Test]
public void CountByEvenOddTest()
{
var result = Enumerable.Range(1, 100).CountBy(c => c % 2);
var result = MoreEnumerable.CountBy(Enumerable.Range(1, 100), c => c % 2);

result.AssertSequenceEqual(
KeyValuePair.Create(1, 50),
Expand All @@ -63,7 +63,7 @@ public void CountByEvenOddTest()
[Test]
public void CountByWithEqualityComparer()
{
var result = new[] { "a", "B", "c", "A", "b", "A" }.CountBy(c => c, StringComparer.OrdinalIgnoreCase);
var result = MoreEnumerable.CountBy(["a", "B", "c", "A", "b", "A"], c => c, StringComparer.OrdinalIgnoreCase);

result.AssertSequenceEqual(
KeyValuePair.Create("a", 3),
Expand All @@ -76,7 +76,7 @@ public void CountByHasKeysOrderedLikeGroupBy()
{
var randomSequence = MoreEnumerable.Random(0, 100).Take(100).ToArray();

var countByKeys = randomSequence.CountBy(x => x).Select(x => x.Key);
var countByKeys = MoreEnumerable.CountBy(randomSequence, x => x).Select(x => x.Key);
var groupByKeys = randomSequence.GroupBy(x => x).Select(x => x.Key);

countByKeys.AssertSequenceEqual(groupByKeys);
Expand All @@ -85,7 +85,7 @@ public void CountByHasKeysOrderedLikeGroupBy()
[Test]
public void CountByIsLazy()
{
_ = new BreakingSequence<string>().CountBy(BreakingFunc.Of<string, int>());
_ = MoreEnumerable.CountBy(new BreakingSequence<string>(), BreakingFunc.Of<string, int>());
}

[Test]
Expand All @@ -95,7 +95,7 @@ public void CountByWithSomeNullKeys()
{
"foo", null, "bar", "baz", null, null, "baz", "bar", null, "foo"
};
var result = ss.CountBy(s => s);
var result = MoreEnumerable.CountBy(ss, s => s);

result.AssertSequenceEqual(
KeyValuePair.Create((string?)"foo", 2),
Expand All @@ -107,7 +107,8 @@ public void CountByWithSomeNullKeys()
[Test]
public void CountByWithSomeNullKeysAndEqualityComparer()
{
var result = new[] { "a", "B", null, "c", "A", null, "b", "A" }.CountBy(c => c, StringComparer.OrdinalIgnoreCase);
string?[] source = ["a", "B", null, "c", "A", null, "b", "A"];
var result = MoreEnumerable.CountBy(source, c => c, StringComparer.OrdinalIgnoreCase);

result.AssertSequenceEqual(
KeyValuePair.Create((string?)"a", 3),
Expand Down
24 changes: 24 additions & 0 deletions MoreLinq/CompatibilitySuppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
<Left>lib/net6.0/MoreLinq.dll</Left>
<Right>lib/net8.0/MoreLinq.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:MoreLinq.Extensions.CountByExtension.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1})</Target>
<Left>lib/net8.0/MoreLinq.dll</Left>
<Right>lib/net9.0/MoreLinq.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:MoreLinq.Extensions.CountByExtension.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})</Target>
<Left>lib/net8.0/MoreLinq.dll</Left>
<Right>lib/net9.0/MoreLinq.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:MoreLinq.MoreEnumerable.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1},System.Collections.Generic.IEqualityComparer{``1})</Target>
<Left>lib/net8.0/MoreLinq.dll</Left>
<Right>lib/net9.0/MoreLinq.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:MoreLinq.MoreEnumerable.CountBy``2(System.Collections.Generic.IEnumerable{``0},System.Func{``0,``1})</Target>
<Left>lib/net8.0/MoreLinq.dll</Left>
<Right>lib/net9.0/MoreLinq.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
<Target>M:MoreLinq.MoreEnumerable.SkipLast``1(System.Collections.Generic.IEnumerable{``0},System.Int32)</Target>
Expand Down
14 changes: 11 additions & 3 deletions MoreLinq/CountBy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ static partial class MoreEnumerable
/// <param name="keySelector">Function that transforms each item of source sequence into a key to be compared against the others.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
#if !NET9_0_OR_GREATER
this
#endif
IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
return source.CountBy(keySelector, null);
return CountBy(source, keySelector, null);
}

/// <summary>
Expand All @@ -50,7 +54,11 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
#if !NET9_0_OR_GREATER
this
#endif
IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Experimental/Await.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ static IEnumerable<TResult> Impl(
var completed = false;
var cancellationTokenSource = new CancellationTokenSource();

var enumerator = source.Index().GetEnumerator();
var enumerator = MoreEnumerable.Index(source).GetEnumerator();
IDisposable disposable = enumerator; // disables AccessToDisposedClosure warnings

try
Expand Down
12 changes: 10 additions & 2 deletions MoreLinq/Extensions.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,11 @@ public static partial class CountByExtension
/// <param name="keySelector">Function that transforms each item of source sequence into a key to be compared against the others.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
#if !NET9_0_OR_GREATER
this
#endif
IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
=> MoreEnumerable.CountBy(source, keySelector);

/// <summary>
Expand All @@ -1189,7 +1193,11 @@ public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this I
/// If null, the default equality comparer for <typeparamref name="TSource"/> is used.</param>
/// <returns>A sequence of unique keys and their number of occurrences in the original sequence.</returns>

public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
#if !NET9_0_OR_GREATER
this
#endif
IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey>? comparer)
=> MoreEnumerable.CountBy(source, keySelector, comparer);

}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/MoreLinq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>4.3.1</VersionPrefix>
<Authors>MoreLINQ Developers.</Authors>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0</TargetFrameworks>
<DebugType>portable</DebugType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>MoreLinq</AssemblyName>
Expand Down
1 change: 1 addition & 0 deletions MoreLinq/PublicAPI/net9.0/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#nullable enable
Loading

0 comments on commit 845294d

Please sign in to comment.