Skip to content

Commit

Permalink
Cleanup Partition Nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
viceroypenguin committed Nov 23, 2022
1 parent c3a7094 commit 26b9656
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
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

0 comments on commit 26b9656

Please sign in to comment.