Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Partition Nullability #881

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MoreLinq.Test/PartitionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// limitations under the License.
#endregion

#nullable enable

namespace MoreLinq.Test
{
using System;
Expand Down
16 changes: 9 additions & 7 deletions MoreLinq/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public static TResult Partition<TKey, TElement, TResult>(this IEnumerable<IGroup
Func<IEnumerable<TElement>, IEnumerable<IGrouping<TKey, TElement>>, TResult> resultSelector)
{
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
return PartitionImpl(source, 1, key, key2: default!, key3: default!, comparer,

return PartitionImpl(source, 1, key, key2: default, key3: default, comparer,
(a, _, _, rest) => resultSelector(a, rest));
}

Expand Down Expand Up @@ -235,8 +236,9 @@ public static TResult Partition<TKey, TElement, TResult>(this IEnumerable<IGroup
Func<IEnumerable<TElement>, IEnumerable<TElement>, IEnumerable<IGrouping<TKey, TElement>>, TResult> resultSelector)
{
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
return PartitionImpl(source, 2, key1, key2, key3: default!, comparer,
(a, b, _, rest) => resultSelector(a, b, rest));

return PartitionImpl(source, 2, key1, key2, key3: default, comparer,
(a, b, c, rest) => resultSelector(a, b, rest));
}

/// <summary>
Expand Down Expand Up @@ -296,7 +298,7 @@ public static TResult Partition<TKey, TElement, TResult>(this IEnumerable<IGroup
PartitionImpl(source, 3, key1, key2, key3, comparer, resultSelector);

static TResult PartitionImpl<TKey, TElement, TResult>(IEnumerable<IGrouping<TKey, TElement>> source,
int count, TKey key1, TKey key2, TKey key3, IEqualityComparer<TKey>? comparer,
int count, TKey? key1, TKey? key2, TKey? key3, IEqualityComparer<TKey>? comparer,
Func<IEnumerable<TElement>, IEnumerable<TElement>, IEnumerable<TElement>, IEnumerable<IGrouping<TKey, TElement>>, TResult> resultSelector)
{
Debug.Assert(count is > 0 and <= 3);
Expand All @@ -317,9 +319,9 @@ static TResult PartitionImpl<TKey, TElement, TResult>(IEnumerable<IGrouping<TKey

foreach (var e in source)
{
var i = count > 0 && comparer.Equals(e.Key, key1) ? 0
: count > 1 && comparer.Equals(e.Key, key2) ? 1
: count > 2 && comparer.Equals(e.Key, key3) ? 2
var i = count > 0 && comparer.Equals(e.Key, key1!) ? 0
: count > 1 && comparer.Equals(e.Key, key2!) ? 1
: count > 2 && comparer.Equals(e.Key, key3!) ? 2
: -1;

if (i < 0)
Expand Down