Skip to content

Commit

Permalink
Revise & take advantage of pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz committed Nov 4, 2022
1 parent d684bf8 commit 1b1b602
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 25 deletions.
4 changes: 2 additions & 2 deletions MoreLinq.Test/AggregateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void SevenUniqueAccumulators()
EvenSum = esum,
Count = count,
Average = (double)sum / count,
Min = min is {} mn ? mn : throw new InvalidOperationException(),
Max = max is {} mx ? mx : throw new InvalidOperationException(),
Min = min ?? throw new InvalidOperationException(),
Max = max ?? throw new InvalidOperationException(),
UniqueLengths = lengths,
Items = items,
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/FlattenTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void FlattenPredicate()
7,
};

var result = source.Flatten(obj => !(obj is IEnumerable<bool>));
var result = source.Flatten(obj => obj is not IEnumerable<bool>);

var expectations = new object[]
{
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/LeadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void TestLeadPassesCorrectValueOffsetBy2()

Assert.AreEqual(count, result.Count());
Assert.IsTrue(result.Take(count - 2).All(x => x.B == (x.A + 2)));
Assert.IsTrue(result.Skip(count - 2).All(x => x.B == leadDefault && (x.A == count || x.A == count - 1)));
Assert.IsTrue(result.Skip(count - 2).All(x => x.B == leadDefault && x.A is count or count - 1));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/MemoizeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ IEnumerable<object> InnerForEach(IEnumerable<int> source)
{
yield return i;

if (i == 3 || i == 7)
if (i is 3 or 7)
{
//consume 1-3 already cached
//add 4-5 to cache (so go to outer loop)
Expand Down
8 changes: 4 additions & 4 deletions MoreLinq.Test/RandomTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public void TestRandomDouble()
// NOTE: Unclear what should actually be verified here... some additional thought needed.
Assert.AreEqual(RandomTrials, resultA.Count());
Assert.AreEqual(RandomTrials, resultB.Count());
Assert.IsTrue(resultA.All(x => x >= 0.0 && x < 1.0));
Assert.IsTrue(resultB.All(x => x >= 0.0 && x < 1.0));
Assert.IsTrue(resultA.All(x => x is >= 0.0 and < 1.0));
Assert.IsTrue(resultB.All(x => x is >= 0.0 and < 1.0));
}

/// <summary>
Expand Down Expand Up @@ -103,8 +103,8 @@ public void TestRandomMinMaxConstraint()

Assert.AreEqual(RandomTrials, resultA.Count());
Assert.AreEqual(RandomTrials, resultB.Count());
Assert.IsTrue(resultA.All(x => x >= min && x < max));
Assert.IsTrue(resultB.All(x => x >= min && x < max));
Assert.IsTrue(resultA.All(x => x is >= min and < max));
Assert.IsTrue(resultB.All(x => x is >= min and < max));
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions MoreLinq.Test/TraceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ static IEnumerable<string> Lines(string str)
IEnumerator<string> _(TextReader reader)
{
Debug.Assert(reader != null);
string line;
while ((line = reader.ReadLine()) != null)
while (reader.ReadLine() is { } line)
yield return line;
}
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Experimental/Await.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public sealed class AwaitQueryOptions

AwaitQueryOptions(int? maxConcurrency, TaskScheduler scheduler, bool preserveOrder)
{
MaxConcurrency = maxConcurrency == null || maxConcurrency > 0
MaxConcurrency = maxConcurrency is null or > 0
? maxConcurrency
: throw new ArgumentOutOfRangeException(
nameof(maxConcurrency), maxConcurrency,
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq/FallbackIfEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ IEnumerable<T> _()

IEnumerable<T> Fallback()
{
return fallback is {} seq ? seq : FallbackOnArgs();
return fallback ?? FallbackOnArgs();

IEnumerable<T> FallbackOnArgs()
{
Debug.Assert(count >= 1 && count <= 4);
Debug.Assert(count is >= 1 and <= 4);

yield return fallback1!;
if (count > 1) yield return fallback2!;
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Flatten.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static IEnumerable<
#nullable restore
/*...................................*/ >
Flatten(this IEnumerable source) =>
Flatten(source, obj => !(obj is string));
Flatten(source, obj => obj is not string);

/// <summary>
/// Flattens a sequence containing arbitrarily-nested sequences. An
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Partition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ static TResult PartitionImpl<TKey, TElement, TResult>(IEnumerable<IGrouping<TKey
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 > 0 && count <= 3);
Debug.Assert(count is > 0 and <= 3);

if (source == null) throw new ArgumentNullException(nameof(source));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Split.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public static IEnumerable<TResult> Split<TSource, TResult>(this IEnumerable<TSou
}
}

if (items != null && items.Count > 0)
if (items is { Count: > 0 })
yield return resultSelector(items);
}
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/Subsets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void ExtractSubset()

public SubsetGenerator(IEnumerable<T> sequence, int subsetSize)
{
if (sequence == null)
if (sequence is null)
throw new ArgumentNullException(nameof(sequence));
if (subsetSize < 0)
throw new ArgumentOutOfRangeException(nameof(subsetSize), "{subsetSize} must be between 0 and set.Count()");
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/ToDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ MemberInfo GetAccessedMember(LambdaExpression lambda)
var body = lambda.Body;

// If it's a field access, boxing was used, we need the field
if (body.NodeType == ExpressionType.Convert || body.NodeType == ExpressionType.ConvertChecked)
if (body.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked)
body = ((UnaryExpression)body).Operand;

// Check if the member expression is valid and is a "first level"
Expand Down
12 changes: 6 additions & 6 deletions bld/ExtensionsGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ where md.ParameterList.Parameters.Count > 0
SortableParameterTypes =
from p in md.ParameterList.Parameters
select CreateTypeKey(p.Type,
n => typeParameterAbbreviationByName != null
&& typeParameterAbbreviationByName.TryGetValue(n, out var a) ? a : null),
n => typeParameterAbbreviationByName is { } someTypeParameterAbbreviationByName
&& someTypeParameterAbbreviationByName.TryGetValue(n, out var a) ? a : null),
}
}
from e in ms.Select((m, i) => (SourceOrder: i + 1, Method: m))
Expand Down Expand Up @@ -385,8 +385,8 @@ abstract class TypeKey : IComparable<TypeKey>
public virtual int CompareTo(TypeKey other)
=> ReferenceEquals(this, other) ? 0
: other == null ? 1
: Parameters.Count.CompareTo(other.Parameters.Count) is {} lc and not 0 ? lc
: string.Compare(Name, other.Name, StringComparison.Ordinal) is {} nc and not 0 ? nc
: Parameters.Count.CompareTo(other.Parameters.Count) is var lc and not 0 ? lc
: string.Compare(Name, other.Name, StringComparison.Ordinal) is var nc and not 0 ? nc
: CompareParameters(other);

protected virtual int CompareParameters(TypeKey other) =>
Expand Down Expand Up @@ -455,10 +455,10 @@ protected override int CompareParameters(TypeKey other)
{
if (other is ArrayTypeKey a)
{
if (Ranks.Count.CompareTo(a.Ranks.Count) is {} rlc and not 0)
if (Ranks.Count.CompareTo(a.Ranks.Count) is var rlc and not 0)
return rlc;
if (Ranks.Zip(a.Ranks, (us, them) => (Us: us, Them: them))
.Aggregate(0, (c, r) => c == 0 ? r.Us.CompareTo(r.Them) : c) is {} rc and not 0)
.Aggregate(0, (c, r) => c == 0 ? r.Us.CompareTo(r.Them) : c) is var rc and not 0)
return rc;
}

Expand Down

0 comments on commit 1b1b602

Please sign in to comment.