Skip to content
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
5 changes: 2 additions & 3 deletions MoreLinq.Test.Aot/ToDataTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ sealed class TestObject(int key)
readonly ImmutableArray<TestObject> testObjects;

public ToDataTableTest() =>
this.testObjects = Enumerable.Range(0, 3)
.Select(i => new TestObject(i))
.ToImmutableArray();
this.testObjects = [..from i in Enumerable.Range(0, 3)
select new TestObject(i)];

[TestMethod]
public void ToDataTableNullMemberExpressionMethod()
Expand Down
5 changes: 2 additions & 3 deletions MoreLinq.Test/AggregateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ from m in typeof(MoreEnumerable).GetMethods(BindingFlags.Public | BindingFlags.S
{
Source = source,
Expectation = sum,
Instantiation = m.MakeGenericMethod(Enumerable.Repeat(typeof(int), m.GetGenericArguments().Length - 1)
.Append(typeof(int[])) // TResult
.ToArray()),
Instantiation = m.MakeGenericMethod([..Enumerable.Repeat(typeof(int), m.GetGenericArguments().Length - 1)
.Append(typeof(int[]))]), // TResult
}
into m
let rst = m.Instantiation.GetParameters().Last().ParameterType
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/BacksertTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IEnumerable<int> Backsert(int[] seq1, int index, int[] seq2)
using var test1 = seq1.AsTestingSequence();
using var test2 = seq2.AsTestingSequence();

return test1.Backsert(test2, index).ToArray();
return [..test1.Backsert(test2, index)];
}
}
}
17 changes: 9 additions & 8 deletions MoreLinq.Test/CartesianTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace MoreLinq.Test
{
using System;
using NUnit.Framework;

/// <summary>
Expand Down Expand Up @@ -122,14 +123,14 @@ public void TestCartesianProductCombinations()
var sequenceA = Enumerable.Range(0, 5);
var sequenceB = Enumerable.Range(0, 5);

var expectedSet = new[]
{
Enumerable.Repeat(false, 5).ToArray(),
Enumerable.Repeat(false, 5).ToArray(),
Enumerable.Repeat(false, 5).ToArray(),
Enumerable.Repeat(false, 5).ToArray(),
Enumerable.Repeat(false, 5).ToArray()
};
bool[][] expectedSet =
[
[..Enumerable.Repeat(false, 5)],
[..Enumerable.Repeat(false, 5)],
[..Enumerable.Repeat(false, 5)],
[..Enumerable.Repeat(false, 5)],
[..Enumerable.Repeat(false, 5)],
];

using var tsA = sequenceA.AsTestingSequence();
using var tsB = sequenceB.AsTestingSequence();
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq.Test/FullGroupJoinTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void FullGroupPreservesOrder(OverloadCase overloadCase)
// Order of joined elements is preserved
foreach (var (key, first, second) in result)
{
first.AssertSequenceEqual(listA.Where(t => t.Item1 == key).ToArray());
second.AssertSequenceEqual(listB.Where(t => t.Item1 == key).ToArray());
first.AssertSequenceEqual([..from t in listA where t.Item1 == key select t]);
second.AssertSequenceEqual([..from t in listB where t.Item1 == key select t]);
}
}

Expand Down
14 changes: 6 additions & 8 deletions MoreLinq.Test/MaximaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public class Take
public string[] ReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Maxima(s => s.Length).Take(count).ToArray();
return [..strings.Maxima(s => s.Length).Take(count)];
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -227,9 +227,8 @@ public string[] ReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.Take(count)
.ToArray();
return [..strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.Take(count)];
}
}

Expand All @@ -242,7 +241,7 @@ public class TakeLast
public string[] TakeLastReturnsMaxima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Maxima(s => s.Length).TakeLast(count).ToArray();
return [..strings.Maxima(s => s.Length).TakeLast(count)];
}

[TestCase(0, 0, ExpectedResult = new string[0] )]
Expand All @@ -256,9 +255,8 @@ public string[] TakeLastReturnsMaxima(int count)
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.TakeLast(count)
.ToArray();
return [..strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
.TakeLast(count)];
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions MoreLinq.Test/MinimaTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public class Take
public string[] ReturnsMinima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Minima(s => s.Length).Take(count).ToArray();
return [..strings.Minima(s => s.Length).Take(count)];
}

[TestCase(0, ExpectedResult = new string[0] )]
Expand All @@ -222,9 +222,8 @@ public string[] ReturnsMinima(int count)
public string[] WithComparerReturnsMinimaPerComparer(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
.Take(count)
.ToArray();
return [..strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
.Take(count)];
}
}

Expand All @@ -240,7 +239,7 @@ public class TakeLast
public string[] ReturnsMinima(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Minima(s => s.Length).TakeLast(count).ToArray();
return [..strings.Minima(s => s.Length).TakeLast(count)];
}

[TestCase(0, ExpectedResult = new string[0] )]
Expand All @@ -250,9 +249,8 @@ public string[] ReturnsMinima(int count)
public string[] WithComparerReturnsMinimaPerComparer(int count)
{
using var strings = SampleData.Strings.AsTestingSequence();
return strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
.TakeLast(count)
.ToArray();
return [..strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
.TakeLast(count)];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/NullArgumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var constraints when constraints.Any(t => t.IsGenericType && t.GetGenericTypeDef
_ => throw new NotImplementedException("NullArgumentTest.InstantiateType")
};

return definition.MakeGenericMethod(typeArguments.ToArray());
return definition.MakeGenericMethod([..typeArguments]);
}

static bool IsReferenceType(ParameterInfo parameter) =>
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/PrependTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void PrependIsLazyInTailSequence()
[TestCaseSource(nameof(PrependManySource))]
public int[] PrependMany(int[] head, int[] tail)
{
return tail.Aggregate(head.AsEnumerable(), MoreEnumerable.Prepend).ToArray();
return [..tail.Aggregate(head.AsEnumerable(), MoreEnumerable.Prepend)];
}

public static IEnumerable<TestCaseData> PrependManySource =>
Expand Down
4 changes: 2 additions & 2 deletions MoreLinq.Test/RepeatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void TestRepeatBehavior()

int[] result;
using (var ts = sequence.AsTestingSequence())
result = ts.Repeat(repeatCount).ToArray();
result = [..ts.Repeat(repeatCount)];

var expectedResult = Enumerable.Empty<int>();
for (var i = 0; i < repeatCount; i++)
Expand Down Expand Up @@ -94,7 +94,7 @@ public void TestRepeatForeverBehaviorManyElementsList()

int[] result;
using (var ts = sequence.AsTestingSequence())
result = ts.Repeat().Take(takeCount).ToArray();
result = [..ts.Repeat().Take(takeCount)];

var expectedResult = Enumerable.Empty<int>();
for (var i = 0; i < repeatCount; i++)
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/SkipUntilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ from e in new[]
public int[] TestSkipUntil(int[] source, int min)
{
using var ts = source.AsTestingSequence();
return ts.SkipUntil(v => v >= min).ToArray();
return [..ts.SkipUntil(v => v >= min)];
}
}
}
4 changes: 2 additions & 2 deletions MoreLinq.Test/SplitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class SplitTest
[Test]
public void SplitWithSeparatorAndResultTransformation()
{
var result = "the quick brown fox".ToCharArray().Split(' ', chars => new string(chars.ToArray()));
var result = "the quick brown fox".ToCharArray().Split(' ', chars => new string([..chars]));
result.AssertSequenceEqual("the", "quick", "brown", "fox");
}

[Test]
public void SplitUptoMaxCount()
{
var result = "the quick brown fox".ToCharArray().Split(' ', 2, chars => new string(chars.ToArray()));
var result = "the quick brown fox".ToCharArray().Split(' ', 2, chars => new string([..chars]));
result.AssertSequenceEqual("the", "quick", "brown fox");
}

Expand Down
2 changes: 2 additions & 0 deletions MoreLinq.Test/TestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ internal static IEnumerable<T> AsSourceKind<T>(this List<T> input, SourceKind so
sourceKind switch
{
SourceKind.Sequence => input.Select(x => x),
#pragma warning disable IDE0028 // Simplify collection initialization (would change semantics)
SourceKind.BreakingList => new BreakingList<T>(input),
SourceKind.BreakingReadOnlyList => new BreakingReadOnlyList<T>(input),
SourceKind.BreakingCollection => new BreakingCollection<T>(input),
SourceKind.BreakingReadOnlyCollection => new BreakingReadOnlyCollection<T>(input),
#pragma warning restore IDE0028 // Simplify collection initialization
_ => throw new ArgumentException(null, nameof(sourceKind))
};
}
Expand Down
19 changes: 16 additions & 3 deletions MoreLinq.Test/ToDataTableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,22 @@ sealed class TestObject(int key)
readonly ImmutableArray<TestObject> testObjects;

public ToDataTableTest() =>
this.testObjects = Enumerable.Range(0, 3)
.Select(i => new TestObject(i))
.ToImmutableArray();
this.testObjects =
#if NET8_0_OR_GREATER
[..
#else
#pragma warning disable IDE0303 // Use array creation expression
ImmutableArray.CreateRange(
#pragma warning restore IDE0303 // Use array creation expression
#endif
from i in Enumerable.Range(0, 3)
select new TestObject(i)
#if NET8_0_OR_GREATER
]
#else
)
#endif
;

[Test]
public void ToDataTableNullMemberExpressionMethod()
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/WindowLeftTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void WindowLeftWithSingleElement()

IList<int>[] result;
using (var ts = sequence.AsTestingSequence())
result = ts.WindowLeft(1).ToArray();
result = [..ts.WindowLeft(1)];

// number of windows should be equal to the source sequence length
Assert.That(result.Length, Is.EqualTo(count));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/WindowRightTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void WindowRightWithSingleElement()

IList<int>[] result;
using (var ts = sequence.AsTestingSequence())
result = ts.WindowRight(1).ToArray();
result = [..ts.WindowRight(1)];

// number of windows should be equal to the source sequence length
Assert.That(result.Length, Is.EqualTo(count));
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq.Test/ZipLongestTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ from e in new[]
{
using var ts1 = first.AsTestingSequence();
using var ts2 = second.AsTestingSequence();
return ts1.ZipLongest(ts2, Tuple.Create).ToArray();
return [..ts1.ZipLongest(ts2, Tuple.Create)];
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/EndsWith.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
return second.TryAsCollectionLike() is { Count: var secondCount }
? first.TryAsCollectionLike() is not { Count: var firstCount } || secondCount <= firstCount
&& EndsWith(second, secondCount)
: EndsWith(secondList = second.ToList(), secondList.Count);
: EndsWith(secondList = [..second], secondList.Count);

bool EndsWith(IEnumerable<T> second, int count) =>
first.TakeLast(count).SequenceEqual(second, comparer);
Expand Down
2 changes: 2 additions & 0 deletions MoreLinq/Experimental/Memoize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static IEnumerable<T> Memoize<T>(this IEnumerable<T> source) =>
ICollection<T> => source,
IReadOnlyCollection<T> => source,
MemoizedEnumerable<T> => source,
#pragma warning disable IDE0306 // Simplify collection initialization (would change semantics)
_ => new MemoizedEnumerable<T>(source),
#pragma warning restore IDE0306 // Simplify collection initialization
};
}

Expand Down
2 changes: 2 additions & 0 deletions MoreLinq/Rank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public static IEnumerable<int> RankBy<TSource, TKey>(this IEnumerable<TSource> s

static IEnumerable<int> _(IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
{
#pragma warning disable IDE0305 // Simplify collection initialization (could be less optimal)
source = source.ToArray(); // avoid enumerating source twice
#pragma warning restore IDE0305 // Simplify collection initialization

var rankDictionary = new Collections.Dictionary<TSource, int>(EqualityComparer<TSource>.Default);
var i = 1;
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/SortedMerge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu

sealed class DisposableGroup<T>(IEnumerable<IEnumerator<T>> iterators) : IDisposable
{
public List<IEnumerator<T>> Iterators { get; } = new(iterators);
public List<IEnumerator<T>> Iterators { get; } = [..iterators];

public IEnumerator<T> this[int index] => Iterators[index];

Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/ToDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static MemberInfo[] PrepareMemberInfos<T>(ICollection<Expression<Func<T, object?
throw new ArgumentException("One of the supplied expressions was null.", nameof(expressions));
try
{
return expressions.Select(GetAccessedMember).ToArray();
return [..from e in expressions select GetAccessedMember(e)];
}
catch (ArgumentException e)
{
Expand Down
14 changes: 6 additions & 8 deletions bld/ExtensionsGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,14 @@ from e in ms.Select((m, i) => (SourceOrder: i + 1, Method: m))
e.Method.Name,
e.Method.TypeParameterCount,
e.Method.ParameterCount,
new TupleTypeKey(ImmutableList.CreateRange(e.Method.SortableParameterTypes))
new TupleTypeKey([..e.Method.SortableParameterTypes])
select new
{
e.Method,
e.SourceOrder,
};

q = q.ToArray();
q = [..q];

if (args.OptDebug)
{
Expand Down Expand Up @@ -336,14 +336,12 @@ static TypeKey CreateTypeKey(TypeSyntax root, Func<string, TypeKey?> abbreviator
?? new SimpleTypeKey(ins.ToString()),
GenericNameSyntax gns =>
new GenericTypeKey(gns.Identifier.ToString(),
ImmutableList.CreateRange(gns.TypeArgumentList.Arguments.Select(Walk))),
[..from arg in gns.TypeArgumentList.Arguments select Walk(arg)]),
ArrayTypeSyntax ats =>
new ArrayTypeKey(Walk(ats.ElementType),
ImmutableList.CreateRange(from rs in ats.RankSpecifiers
select rs.Rank)),
[..from rs in ats.RankSpecifiers select rs.Rank]),
TupleTypeSyntax tts =>
new TupleTypeKey(ImmutableList.CreateRange(from te in tts.Elements
select Walk(te.Type))),
new TupleTypeKey([..from te in tts.Elements select Walk(te.Type)]),
_ => throw new NotSupportedException("Unhandled type: " + ts)
};
}
Expand Down Expand Up @@ -415,7 +413,7 @@ public override string ToString() =>
sealed class ArrayTypeKey(TypeKey element, IEnumerable<int> ranks) :
ParameterizedTypeKey("[]", element)
{
public ImmutableList<int> Ranks { get; } = ImmutableList.CreateRange(ranks);
public ImmutableList<int> Ranks { get; } = [..ranks];

public override string ToString() =>
Parameters.Single() + string.Concat(from r in Ranks
Expand Down
Loading