diff --git a/MoreLinq.Test/MoreLinq.Test.csproj b/MoreLinq.Test/MoreLinq.Test.csproj
index ea2da2dde..a08121f0e 100644
--- a/MoreLinq.Test/MoreLinq.Test.csproj
+++ b/MoreLinq.Test/MoreLinq.Test.csproj
@@ -40,7 +40,6 @@
-
diff --git a/MoreLinq/Assert.cs b/MoreLinq/Assert.cs
index 722313366..1b272d8ed 100644
--- a/MoreLinq/Assert.cs
+++ b/MoreLinq/Assert.cs
@@ -60,7 +60,7 @@ public static IEnumerable Assert(this IEnumerable sou
///
public static IEnumerable Assert(this IEnumerable source,
- Func predicate, Func errorSelector)
+ Func predicate, Func? errorSelector)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
diff --git a/MoreLinq/Batch.cs b/MoreLinq/Batch.cs
index c6db41d82..2b63b3f50 100644
--- a/MoreLinq/Batch.cs
+++ b/MoreLinq/Batch.cs
@@ -116,7 +116,7 @@ public static IEnumerable Batch(this IEnumerable Batch(int size)
{
- TSource[] bucket = null;
+ TSource[]? bucket = null;
var count = 0;
foreach (var item in source)
diff --git a/MoreLinq/CountBy.cs b/MoreLinq/CountBy.cs
index 11e22ba29..623fc14a8 100644
--- a/MoreLinq/CountBy.cs
+++ b/MoreLinq/CountBy.cs
@@ -50,7 +50,7 @@ public static IEnumerable> CountBy(this I
/// If null, the default equality comparer for is used.
/// A sequence of unique keys and their number of occurrences in the original sequence.
- public static IEnumerable> CountBy(this IEnumerable source, Func keySelector, IEqualityComparer comparer)
+ public static IEnumerable> CountBy(this IEnumerable source, Func keySelector, IEqualityComparer? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
@@ -94,8 +94,7 @@ bool TryGetIndex(TKey key, out int i)
keys = new List();
counts = new List();
- var havePrevKey = false;
- var prevKey = default(TKey);
+ (bool, TKey) prevKey = (false, default);
var index = 0;
foreach (var item in source)
@@ -103,8 +102,9 @@ bool TryGetIndex(TKey key, out int i)
var key = keySelector(item);
if (// key same as the previous? then re-use the index
- havePrevKey && cmp.GetHashCode(prevKey) == cmp.GetHashCode(key)
- && cmp.Equals(prevKey, key)
+ prevKey is (true, var pk)
+ && cmp.GetHashCode(pk) == cmp.GetHashCode(key)
+ && cmp.Equals(pk, key)
// otherwise try & find index of the key
|| TryGetIndex(key, out index))
{
@@ -121,8 +121,7 @@ bool TryGetIndex(TKey key, out int i)
counts.Add(1);
}
- prevKey = key;
- havePrevKey = true;
+ prevKey = (true, key);
}
}
}
diff --git a/MoreLinq/Delegating.cs b/MoreLinq/Delegating.cs
index 6ccbf6885..4b9e8c189 100644
--- a/MoreLinq/Delegating.cs
+++ b/MoreLinq/Delegating.cs
@@ -33,8 +33,8 @@ public static IDisposable Disposable(Action delegatee) =>
new DelegatingDisposable(delegatee);
public static IObserver Observer(Action onNext,
- Action onError = null,
- Action onCompleted = null) =>
+ Action? onError = null,
+ Action? onCompleted = null) =>
new DelegatingObserver(onNext, onError, onCompleted);
}
@@ -48,7 +48,7 @@ public DelegatingDisposable(Action delegatee) =>
public void Dispose()
{
var delegatee = _delegatee;
- if (delegatee == null || Interlocked.CompareExchange(ref _delegatee, null, delegatee) != delegatee)
+ if (delegatee == null || Interlocked.CompareExchange(ref _delegatee, null!, delegatee) != delegatee)
return;
delegatee();
}
@@ -57,12 +57,12 @@ public void Dispose()
sealed class DelegatingObserver : IObserver
{
readonly Action _onNext;
- readonly Action _onError;
- readonly Action _onCompleted;
+ readonly Action? _onError;
+ readonly Action? _onCompleted;
public DelegatingObserver(Action onNext,
- Action onError = null,
- Action onCompleted = null)
+ Action? onError = null,
+ Action? onCompleted = null)
{
_onNext = onNext ?? throw new ArgumentNullException(nameof(onNext));
_onError = onError;
diff --git a/MoreLinq/DistinctBy.cs b/MoreLinq/DistinctBy.cs
index c8dc010a4..a05faab82 100644
--- a/MoreLinq/DistinctBy.cs
+++ b/MoreLinq/DistinctBy.cs
@@ -63,7 +63,7 @@ public static IEnumerable DistinctBy(this IEnumerable
public static IEnumerable DistinctBy(this IEnumerable source,
- Func keySelector, IEqualityComparer comparer)
+ Func keySelector, IEqualityComparer? comparer)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (keySelector == null) throw new ArgumentNullException(nameof(keySelector));
diff --git a/MoreLinq/EndsWith.cs b/MoreLinq/EndsWith.cs
index ca1de4e66..c6b449351 100644
--- a/MoreLinq/EndsWith.cs
+++ b/MoreLinq/EndsWith.cs
@@ -66,7 +66,7 @@ public static bool EndsWith(this IEnumerable first, IEnumerable second)
/// elements at the same index.
///
- public static bool EndsWith(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
+ public static bool EndsWith(this IEnumerable first, IEnumerable second, IEqualityComparer? comparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
@@ -77,13 +77,13 @@ public static bool EndsWith(this IEnumerable first, IEnumerable second,
return second.TryGetCollectionCount() is int secondCount
? first.TryGetCollectionCount() is int firstCount && secondCount > firstCount
? false
- : Impl(second, secondCount)
- : Impl(secondList = second.ToList(), secondList.Count);
+ : Impl(second, secondCount, comparer)
+ : Impl(secondList = second.ToList(), secondList.Count, comparer);
- bool Impl(IEnumerable snd, int count)
+ bool Impl(IEnumerable snd, int count, IEqualityComparer cmp)
{
using var firstIter = first.TakeLast(count).GetEnumerator();
- return snd.All(item => firstIter.MoveNext() && comparer.Equals(firstIter.Current, item));
+ return snd.All(item => firstIter.MoveNext() && cmp.Equals(firstIter.Current, item));
}
}
}
diff --git a/MoreLinq/EquiZip.cs b/MoreLinq/EquiZip.cs
index edede6ef1..e7b990038 100644
--- a/MoreLinq/EquiZip.cs
+++ b/MoreLinq/EquiZip.cs
@@ -169,15 +169,12 @@ public static IEnumerable EquiZip(
}
static IEnumerable EquiZipImpl(
- IEnumerable s1,
- IEnumerable s2,
- IEnumerable s3,
- IEnumerable s4,
+ IEnumerable s1,
+ IEnumerable s2,
+ IEnumerable? s3,
+ IEnumerable? s4,
Func resultSelector)
{
- Debug.Assert(s1 != null);
- Debug.Assert(s2 != null);
-
const int zero = 0, one = 1;
var limit = 1 + (s3 != null ? one : zero)
diff --git a/MoreLinq/ExceptBy.cs b/MoreLinq/ExceptBy.cs
index d204ce8b0..d3faa6ea8 100644
--- a/MoreLinq/ExceptBy.cs
+++ b/MoreLinq/ExceptBy.cs
@@ -73,7 +73,7 @@ public static IEnumerable ExceptBy(this IEnumerable ExceptBy(this IEnumerable first,
IEnumerable second,
Func keySelector,
- IEqualityComparer keyComparer)
+ IEqualityComparer? keyComparer)
{
if (first == null) throw new ArgumentNullException(nameof(first));
if (second == null) throw new ArgumentNullException(nameof(second));
diff --git a/MoreLinq/Experimental/Await.cs b/MoreLinq/Experimental/Await.cs
index 229fccbcb..dd664c1a8 100644
--- a/MoreLinq/Experimental/Await.cs
+++ b/MoreLinq/Experimental/Await.cs
@@ -28,6 +28,7 @@ namespace MoreLinq.Experimental
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
+ using Unit = System.ValueTuple;
///
/// Represents options for a query whose results evaluate asynchronously.
@@ -436,14 +437,14 @@ IEnumerable _(int? maxConcurrency, TaskScheduler scheduler, bool ordere
// BlockingCollection.Add throws if called after CompleteAdding
// and we want to deliberately tolerate the race condition.
- var notices = new BlockingCollection<(Notice, (int, T, Task), ExceptionDispatchInfo)>();
+ var notices = new BlockingCollection<(Notice, (int, T, Task), ExceptionDispatchInfo?)>();
var consumerCancellationTokenSource = new CancellationTokenSource();
- (Exception, Exception) lastCriticalErrors = default;
+ (Exception?, Exception?) lastCriticalErrors = default;
void PostNotice(Notice notice,
(int, T, Task) item,
- Exception error)
+ Exception? error)
{
// If a notice fails to post then assume critical error
// conditions (like low memory), capture the error without
@@ -532,10 +533,12 @@ await enumerator.StartAsync(
: new AggregateException(error1));
}
- var (kind, result, error) = notice.Current;
+ (Notice kind, (int, T, Task) result, ExceptionDispatchInfo? error) = notice.Current;
if (kind == Notice.Error)
- error.Throw();
+ {
+ error?.Throw();
+ }
if (kind == Notice.End)
break;
@@ -584,7 +587,7 @@ await enumerator.StartAsync(
}
}
- if (holds?.Count > 0) // yield any withheld, which should be in order...
+ if (holds != null && holds.Count > 0) // yield any withheld, which should be in order...
{
foreach (var (key, x, value) in holds)
{
@@ -688,7 +691,7 @@ static class AwaitQuery
public static IAwaitQuery
Create(
Func> impl,
- AwaitQueryOptions options = null) =>
+ AwaitQueryOptions? options = null) =>
new AwaitQuery(impl, options);
}
@@ -697,7 +700,7 @@ sealed class AwaitQuery : IAwaitQuery
readonly Func> _impl;
public AwaitQuery(Func> impl,
- AwaitQueryOptions options = null)
+ AwaitQueryOptions? options = null)
{
_impl = impl;
Options = options ?? AwaitQueryOptions.Default;
@@ -735,8 +738,8 @@ static class CompletedTask
static CompletedTask()
{
- var tcs = new TaskCompletionSource
- public static IEnumerable Flatten(this IEnumerable source, Func selector)
+ public static IEnumerable Flatten(this IEnumerable source, Func selector)
=> MoreEnumerable.Flatten(source, selector);
}
@@ -2533,7 +2535,7 @@ public static IEnumerable FullJoin(
Func firstSelector,
Func secondSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.FullJoin(first, second, keySelector, firstSelector, secondSelector, bothSelector, comparer);
///
@@ -2628,7 +2630,7 @@ public static IEnumerable FullJoin(
Func firstSelector,
Func secondSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.FullJoin(first, second, firstKeySelector, secondKeySelector, firstSelector, secondSelector, bothSelector, comparer);
}
@@ -2693,7 +2695,7 @@ public static IEnumerable> GroupAdjacent
public static IEnumerable> GroupAdjacent(
this IEnumerable source,
Func keySelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.GroupAdjacent(source, keySelector, comparer);
///
@@ -2796,7 +2798,7 @@ public static IEnumerable> GroupAdjacent source,
Func keySelector,
Func elementSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.GroupAdjacent(source, keySelector, elementSelector, comparer);
///
@@ -2832,7 +2834,7 @@ public static IEnumerable GroupAdjacent(
this IEnumerable source,
Func keySelector,
Func, TResult> resultSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.GroupAdjacent(source, keySelector, resultSelector, comparer);
}
@@ -2923,7 +2925,7 @@ public static IEnumerable>
IndexBy(
this IEnumerable source,
Func keySelector,
- IEqualityComparer comparer) => MoreEnumerable. IndexBy(source, keySelector, comparer);
+ IEqualityComparer? comparer) => MoreEnumerable. IndexBy(source, keySelector, comparer);
}
@@ -3203,7 +3205,7 @@ public static IEnumerable LeftJoin(
Func keySelector,
Func firstSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.LeftJoin(first, second, keySelector, firstSelector, bothSelector, comparer);
///
@@ -3288,7 +3290,7 @@ public static IEnumerable LeftJoin(
Func secondKeySelector,
Func firstSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.LeftJoin(first, second, firstKeySelector, secondKeySelector, firstSelector, bothSelector, comparer);
}
@@ -3337,7 +3339,7 @@ public static IExtremaEnumerable MaxBy(this IEnumerable<
/// or is null
public static IExtremaEnumerable MaxBy(this IEnumerable source,
- Func selector, IComparer comparer)
+ Func selector, IComparer? comparer)
=> MoreEnumerable.MaxBy(source, selector, comparer);
}
@@ -3385,7 +3387,7 @@ public static IExtremaEnumerable MinBy(this IEnumerable<
/// or is null
public static IExtremaEnumerable MinBy(this IEnumerable source,
- Func selector, IComparer comparer)
+ Func selector, IComparer? comparer)
=> MoreEnumerable.MinBy(source, selector, comparer);
}
@@ -3454,7 +3456,7 @@ public static IOrderedEnumerable OrderBy(this IEnumerable source,
/// A comparer used to define the semantics of element comparison
/// An ordered copy of the source sequence
- public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer comparer, OrderByDirection direction)
+ public static IOrderedEnumerable OrderBy(this IEnumerable source, Func keySelector, IComparer? comparer, OrderByDirection direction)
=> MoreEnumerable.OrderBy(source, keySelector, comparer, direction);
}
@@ -3508,7 +3510,7 @@ public static IEnumerable OrderedMerge(
public static IEnumerable OrderedMerge(
this IEnumerable first,
IEnumerable second,
- IComparer comparer)
+ IComparer? comparer)
=> MoreEnumerable.OrderedMerge(first, second, comparer);
///
@@ -3612,7 +3614,7 @@ public static IEnumerable OrderedMerge(
Func firstSelector,
Func secondSelector,
Func bothSelector,
- IComparer comparer)
+ IComparer? comparer)
=> MoreEnumerable.OrderedMerge(first, second, keySelector, firstSelector, secondSelector, bothSelector, comparer);
///
@@ -3701,7 +3703,7 @@ public static IEnumerable OrderedMerge(
Func firstSelector,
Func secondSelector,
Func bothSelector,
- IComparer comparer)
+ IComparer? comparer)
=> MoreEnumerable.OrderedMerge(first, second, firstKeySelector, secondKeySelector, firstSelector, secondSelector, bothSelector, comparer);
}
@@ -3976,7 +3978,7 @@ public static IEnumerable PartialSort(this IEnumerable source,
///
public static IEnumerable PartialSort(this IEnumerable source,
- int count, IComparer comparer)
+ int count, IComparer? comparer)
=> MoreEnumerable.PartialSort(source, count, comparer);
///
@@ -3998,7 +4000,7 @@ public static IEnumerable PartialSort(this IEnumerable source,
///
public static IEnumerable PartialSort(this IEnumerable source,
- int count, IComparer comparer, OrderByDirection direction)
+ int count, IComparer? comparer, OrderByDirection direction)
=> MoreEnumerable.PartialSort(source, count, comparer, direction);
}
@@ -4071,7 +4073,7 @@ public static IEnumerable PartialSortBy(
public static IEnumerable PartialSortBy(
this IEnumerable source, int count,
Func keySelector,
- IComparer comparer)
+ IComparer? comparer)
=> MoreEnumerable.PartialSortBy(source, count, keySelector, comparer);
///
@@ -4096,7 +4098,7 @@ public static IEnumerable PartialSortBy(
public static IEnumerable PartialSortBy(
this IEnumerable source, int count,
Func keySelector,
- IComparer comparer,
+ IComparer? comparer,
OrderByDirection direction)
=> MoreEnumerable.PartialSortBy(source, count, keySelector, comparer, direction);
@@ -4270,7 +4272,7 @@ public static TResult Partition(this IEnumerable
public static TResult Partition(this IEnumerable> source,
- TKey key, IEqualityComparer comparer,
+ TKey key, IEqualityComparer? comparer,
Func, IEnumerable>, TResult> resultSelector)
=> MoreEnumerable.Partition(source, key, comparer, resultSelector);
@@ -4324,7 +4326,7 @@ public static TResult Partition(this IEnumerable
public static TResult Partition(this IEnumerable> source,
- TKey key1, TKey key2, IEqualityComparer comparer,
+ TKey key1, TKey key2, IEqualityComparer? comparer,
Func, IEnumerable, IEnumerable>, TResult> resultSelector)
=> MoreEnumerable.Partition(source, key1, key2, comparer, resultSelector);
@@ -4353,7 +4355,7 @@ public static TResult Partition(this IEnumerable
public static TResult Partition(this IEnumerable> source,
- TKey key1, TKey key2, TKey key3, IEqualityComparer comparer,
+ TKey key1, TKey key2, TKey key3, IEqualityComparer? comparer,
Func, IEnumerable, IEnumerable, IEnumerable>, TResult> resultSelector) => MoreEnumerable.Partition(source, key1, key2, key3, comparer, resultSelector);
}
@@ -4577,7 +4579,7 @@ public static IEnumerable RankBy(this IEnumerable s
/// An object that defines the comparison semantics for keys used to rank items
/// A sequence of position integers representing the ranks of the corresponding items in the sequence
- public static IEnumerable RankBy(this IEnumerable source, Func keySelector, IComparer comparer)
+ public static IEnumerable RankBy(this IEnumerable source, Func keySelector, IComparer? comparer)
=> MoreEnumerable.RankBy(source, keySelector, comparer);
}
@@ -4690,7 +4692,7 @@ public static IEnumerable RightJoin(
Func keySelector,
Func secondSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.RightJoin(first, second, keySelector, secondSelector, bothSelector, comparer);
///
@@ -4775,7 +4777,7 @@ public static IEnumerable RightJoin(
Func secondKeySelector,
Func secondSelector,
Func bothSelector,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.RightJoin(first, second, firstKeySelector, secondKeySelector, secondSelector, bothSelector, comparer);
}
@@ -4806,7 +4808,7 @@ public static IEnumerable> RunLengthEncode(this IEnumera
/// The comparer used to identify equivalent items
/// A sequence of KeyValuePair{T,int} where they key is the element and the value is the occurrence count
- public static IEnumerable> RunLengthEncode(this IEnumerable sequence, IEqualityComparer comparer)
+ public static IEnumerable> RunLengthEncode(this IEnumerable sequence, IEqualityComparer? comparer)
=> MoreEnumerable.RunLengthEncode(sequence, comparer);
}
@@ -4933,7 +4935,7 @@ public static IEnumerable> ScanBy keySelector,
Func seedSelector,
Func accumulator,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.ScanBy(source, keySelector, seedSelector, accumulator, comparer);
}
@@ -5275,7 +5277,7 @@ public static IEnumerable SortedMerge(this IEnumerableA variable argument array of zero or more other sequences to merge with
/// A merged, order-preserving sequence containing al of the elements of the original sequences
- public static IEnumerable SortedMerge(this IEnumerable source, OrderByDirection direction, IComparer comparer, params IEnumerable[] otherSequences)
+ public static IEnumerable SortedMerge(this IEnumerable source, OrderByDirection direction, IComparer? comparer, params IEnumerable[] otherSequences)
=> MoreEnumerable.SortedMerge(source, direction, comparer, otherSequences);
}
@@ -5337,7 +5339,7 @@ public static IEnumerable> Split(this IEnumerable<
/// A sequence of splits of elements.
public static IEnumerable> Split(this IEnumerable source,
- TSource separator, IEqualityComparer comparer)
+ TSource separator, IEqualityComparer? comparer)
=> MoreEnumerable.Split(source, separator, comparer);
///
@@ -5369,7 +5371,7 @@ public static IEnumerable> Split(this IEnumerable<
/// A sequence of splits of elements.
public static IEnumerable> Split(this IEnumerable source,
- TSource separator, IEqualityComparer comparer, int count)
+ TSource separator, IEqualityComparer? comparer, int count)
=> MoreEnumerable.Split(source, separator, comparer, count);
///
@@ -5494,7 +5496,7 @@ public static IEnumerable Split(this IEnumerable
public static IEnumerable Split(this IEnumerable source,
- TSource separator, IEqualityComparer comparer, int count,
+ TSource separator, IEqualityComparer? comparer, int count,
Func, TResult> resultSelector)
=> MoreEnumerable.Split(source, separator, comparer, count, resultSelector);
@@ -5548,7 +5550,7 @@ public static bool StartsWith(this IEnumerable first, IEnumerable secon
/// of elements at the same index.
///
- public static bool StartsWith(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
+ public static bool StartsWith(this IEnumerable first, IEnumerable second, IEqualityComparer? comparer)
=> MoreEnumerable.StartsWith(first, second, comparer);
}
@@ -5776,7 +5778,7 @@ public static IOrderedEnumerable ThenBy(this IOrderedEnumerable s
/// A comparer used to define the semantics of element comparison
/// An ordered copy of the source sequence
- public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector, IComparer comparer, OrderByDirection direction)
+ public static IOrderedEnumerable ThenBy(this IOrderedEnumerable source, Func keySelector, IComparer? comparer, OrderByDirection direction)
=> MoreEnumerable.ThenBy(source, keySelector, comparer, direction);
}
@@ -6354,7 +6356,7 @@ public static partial class ToDictionaryExtension
///
public static Dictionary ToDictionary(this IEnumerable<(TKey Key, TValue Value)> source,
- IEqualityComparer comparer)
+ IEqualityComparer? comparer)
=> MoreEnumerable.ToDictionary(source, comparer);
///
@@ -6372,7 +6374,7 @@ public static Dictionary ToDictionary