Skip to content

Commit 7289b23

Browse files

23 files changed

+69
-57
lines changed

MoreLinq.Test.Aot/ToDataTableTest.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ sealed class TestObject(int key)
4545
readonly ImmutableArray<TestObject> testObjects;
4646

4747
public ToDataTableTest() =>
48-
this.testObjects = Enumerable.Range(0, 3)
49-
.Select(i => new TestObject(i))
50-
.ToImmutableArray();
48+
this.testObjects = [..from i in Enumerable.Range(0, 3)
49+
select new TestObject(i)];
5150

5251
[TestMethod]
5352
public void ToDataTableNullMemberExpressionMethod()

MoreLinq.Test/AggregateTest.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ from m in typeof(MoreEnumerable).GetMethods(BindingFlags.Public | BindingFlags.S
5151
{
5252
Source = source,
5353
Expectation = sum,
54-
Instantiation = m.MakeGenericMethod(Enumerable.Repeat(typeof(int), m.GetGenericArguments().Length - 1)
55-
.Append(typeof(int[])) // TResult
56-
.ToArray()),
54+
Instantiation = m.MakeGenericMethod([..Enumerable.Repeat(typeof(int), m.GetGenericArguments().Length - 1)
55+
.Append(typeof(int[]))]), // TResult
5756
}
5857
into m
5958
let rst = m.Instantiation.GetParameters().Last().ParameterType

MoreLinq.Test/BacksertTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IEnumerable<int> Backsert(int[] seq1, int index, int[] seq2)
5757
using var test1 = seq1.AsTestingSequence();
5858
using var test2 = seq2.AsTestingSequence();
5959

60-
return test1.Backsert(test2, index).ToArray();
60+
return [..test1.Backsert(test2, index)];
6161
}
6262
}
6363
}

MoreLinq.Test/CartesianTest.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace MoreLinq.Test
1919
{
20+
using System;
2021
using NUnit.Framework;
2122

2223
/// <summary>
@@ -122,14 +123,14 @@ public void TestCartesianProductCombinations()
122123
var sequenceA = Enumerable.Range(0, 5);
123124
var sequenceB = Enumerable.Range(0, 5);
124125

125-
var expectedSet = new[]
126-
{
127-
Enumerable.Repeat(false, 5).ToArray(),
128-
Enumerable.Repeat(false, 5).ToArray(),
129-
Enumerable.Repeat(false, 5).ToArray(),
130-
Enumerable.Repeat(false, 5).ToArray(),
131-
Enumerable.Repeat(false, 5).ToArray()
132-
};
126+
bool[][] expectedSet =
127+
[
128+
[..Enumerable.Repeat(false, 5)],
129+
[..Enumerable.Repeat(false, 5)],
130+
[..Enumerable.Repeat(false, 5)],
131+
[..Enumerable.Repeat(false, 5)],
132+
[..Enumerable.Repeat(false, 5)],
133+
];
133134

134135
using var tsA = sequenceA.AsTestingSequence();
135136
using var tsB = sequenceB.AsTestingSequence();

MoreLinq.Test/FullGroupJoinTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ public void FullGroupPreservesOrder(OverloadCase overloadCase)
126126
// Order of joined elements is preserved
127127
foreach (var (key, first, second) in result)
128128
{
129-
first.AssertSequenceEqual(listA.Where(t => t.Item1 == key).ToArray());
130-
second.AssertSequenceEqual(listB.Where(t => t.Item1 == key).ToArray());
129+
first.AssertSequenceEqual([..from t in listA where t.Item1 == key select t]);
130+
second.AssertSequenceEqual([..from t in listB where t.Item1 == key select t]);
131131
}
132132
}
133133

MoreLinq.Test/MaximaTest.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public class Take
213213
public string[] ReturnsMaxima(int count)
214214
{
215215
using var strings = SampleData.Strings.AsTestingSequence();
216-
return strings.Maxima(s => s.Length).Take(count).ToArray();
216+
return [..strings.Maxima(s => s.Length).Take(count)];
217217
}
218218

219219
[TestCase(0, 0, ExpectedResult = new string[0] )]
@@ -227,9 +227,8 @@ public string[] ReturnsMaxima(int count)
227227
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
228228
{
229229
using var strings = SampleData.Strings.AsTestingSequence();
230-
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
231-
.Take(count)
232-
.ToArray();
230+
return [..strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
231+
.Take(count)];
233232
}
234233
}
235234

@@ -242,7 +241,7 @@ public class TakeLast
242241
public string[] TakeLastReturnsMaxima(int count)
243242
{
244243
using var strings = SampleData.Strings.AsTestingSequence();
245-
return strings.Maxima(s => s.Length).TakeLast(count).ToArray();
244+
return [..strings.Maxima(s => s.Length).TakeLast(count)];
246245
}
247246

248247
[TestCase(0, 0, ExpectedResult = new string[0] )]
@@ -256,9 +255,8 @@ public string[] TakeLastReturnsMaxima(int count)
256255
public string[] WithComparerReturnsMaximaPerComparer(int count, int index)
257256
{
258257
using var strings = SampleData.Strings.AsTestingSequence();
259-
return strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
260-
.TakeLast(count)
261-
.ToArray();
258+
return [..strings.Maxima(s => s[index], Comparable<char>.DescendingOrderComparer)
259+
.TakeLast(count)];
262260
}
263261
}
264262
}

MoreLinq.Test/MinimaTest.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public class Take
212212
public string[] ReturnsMinima(int count)
213213
{
214214
using var strings = SampleData.Strings.AsTestingSequence();
215-
return strings.Minima(s => s.Length).Take(count).ToArray();
215+
return [..strings.Minima(s => s.Length).Take(count)];
216216
}
217217

218218
[TestCase(0, ExpectedResult = new string[0] )]
@@ -222,9 +222,8 @@ public string[] ReturnsMinima(int count)
222222
public string[] WithComparerReturnsMinimaPerComparer(int count)
223223
{
224224
using var strings = SampleData.Strings.AsTestingSequence();
225-
return strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
226-
.Take(count)
227-
.ToArray();
225+
return [..strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
226+
.Take(count)];
228227
}
229228
}
230229

@@ -240,7 +239,7 @@ public class TakeLast
240239
public string[] ReturnsMinima(int count)
241240
{
242241
using var strings = SampleData.Strings.AsTestingSequence();
243-
return strings.Minima(s => s.Length).TakeLast(count).ToArray();
242+
return [..strings.Minima(s => s.Length).TakeLast(count)];
244243
}
245244

246245
[TestCase(0, ExpectedResult = new string[0] )]
@@ -250,9 +249,8 @@ public string[] ReturnsMinima(int count)
250249
public string[] WithComparerReturnsMinimaPerComparer(int count)
251250
{
252251
using var strings = SampleData.Strings.AsTestingSequence();
253-
return strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
254-
.TakeLast(count)
255-
.ToArray();
252+
return [..strings.Minima(s => s.Length, Comparable<int>.DescendingOrderComparer)
253+
.TakeLast(count)];
256254
}
257255
}
258256
}

MoreLinq.Test/NullArgumentTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var constraints when constraints.Any(t => t.IsGenericType && t.GetGenericTypeDef
103103
_ => throw new NotImplementedException("NullArgumentTest.InstantiateType")
104104
};
105105

106-
return definition.MakeGenericMethod(typeArguments.ToArray());
106+
return definition.MakeGenericMethod([..typeArguments]);
107107
}
108108

109109
static bool IsReferenceType(ParameterInfo parameter) =>

MoreLinq.Test/PrependTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void PrependIsLazyInTailSequence()
6060
[TestCaseSource(nameof(PrependManySource))]
6161
public int[] PrependMany(int[] head, int[] tail)
6262
{
63-
return tail.Aggregate(head.AsEnumerable(), MoreEnumerable.Prepend).ToArray();
63+
return [..tail.Aggregate(head.AsEnumerable(), MoreEnumerable.Prepend)];
6464
}
6565

6666
public static IEnumerable<TestCaseData> PrependManySource =>

MoreLinq.Test/RepeatTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TestRepeatBehavior()
4646

4747
int[] result;
4848
using (var ts = sequence.AsTestingSequence())
49-
result = ts.Repeat(repeatCount).ToArray();
49+
result = [..ts.Repeat(repeatCount)];
5050

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

9595
int[] result;
9696
using (var ts = sequence.AsTestingSequence())
97-
result = ts.Repeat().Take(takeCount).ToArray();
97+
result = [..ts.Repeat().Take(takeCount)];
9898

9999
var expectedResult = Enumerable.Empty<int>();
100100
for (var i = 0; i < repeatCount; i++)

MoreLinq.Test/SkipUntilTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ from e in new[]
7777
public int[] TestSkipUntil(int[] source, int min)
7878
{
7979
using var ts = source.AsTestingSequence();
80-
return ts.SkipUntil(v => v >= min).ToArray();
80+
return [..ts.SkipUntil(v => v >= min)];
8181
}
8282
}
8383
}

MoreLinq.Test/SplitTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public class SplitTest
2525
[Test]
2626
public void SplitWithSeparatorAndResultTransformation()
2727
{
28-
var result = "the quick brown fox".ToCharArray().Split(' ', chars => new string(chars.ToArray()));
28+
var result = "the quick brown fox".ToCharArray().Split(' ', chars => new string([..chars]));
2929
result.AssertSequenceEqual("the", "quick", "brown", "fox");
3030
}
3131

3232
[Test]
3333
public void SplitUptoMaxCount()
3434
{
35-
var result = "the quick brown fox".ToCharArray().Split(' ', 2, chars => new string(chars.ToArray()));
35+
var result = "the quick brown fox".ToCharArray().Split(' ', 2, chars => new string([..chars]));
3636
result.AssertSequenceEqual("the", "quick", "brown fox");
3737
}
3838

MoreLinq.Test/TestExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ internal static IEnumerable<T> AsSourceKind<T>(this List<T> input, SourceKind so
9797
sourceKind switch
9898
{
9999
SourceKind.Sequence => input.Select(x => x),
100+
#pragma warning disable IDE0028 // Simplify collection initialization (would change semantics)
100101
SourceKind.BreakingList => new BreakingList<T>(input),
101102
SourceKind.BreakingReadOnlyList => new BreakingReadOnlyList<T>(input),
102103
SourceKind.BreakingCollection => new BreakingCollection<T>(input),
103104
SourceKind.BreakingReadOnlyCollection => new BreakingReadOnlyCollection<T>(input),
105+
#pragma warning restore IDE0028 // Simplify collection initialization
104106
_ => throw new ArgumentException(null, nameof(sourceKind))
105107
};
106108
}

MoreLinq.Test/ToDataTableTest.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,22 @@ sealed class TestObject(int key)
4444
readonly ImmutableArray<TestObject> testObjects;
4545

4646
public ToDataTableTest() =>
47-
this.testObjects = Enumerable.Range(0, 3)
48-
.Select(i => new TestObject(i))
49-
.ToImmutableArray();
47+
this.testObjects =
48+
#if NET8_0_OR_GREATER
49+
[..
50+
#else
51+
#pragma warning disable IDE0303 // Use array creation expression
52+
ImmutableArray.CreateRange(
53+
#pragma warning restore IDE0303 // Use array creation expression
54+
#endif
55+
from i in Enumerable.Range(0, 3)
56+
select new TestObject(i)
57+
#if NET8_0_OR_GREATER
58+
]
59+
#else
60+
)
61+
#endif
62+
;
5063

5164
[Test]
5265
public void ToDataTableNullMemberExpressionMethod()

MoreLinq.Test/WindowLeftTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void WindowLeftWithSingleElement()
9393

9494
IList<int>[] result;
9595
using (var ts = sequence.AsTestingSequence())
96-
result = ts.WindowLeft(1).ToArray();
96+
result = [..ts.WindowLeft(1)];
9797

9898
// number of windows should be equal to the source sequence length
9999
Assert.That(result.Length, Is.EqualTo(count));

MoreLinq.Test/WindowRightTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void WindowRightWithSingleElement()
9393

9494
IList<int>[] result;
9595
using (var ts = sequence.AsTestingSequence())
96-
result = ts.WindowRight(1).ToArray();
96+
result = [..ts.WindowRight(1)];
9797

9898
// number of windows should be equal to the source sequence length
9999
Assert.That(result.Length, Is.EqualTo(count));

MoreLinq.Test/ZipLongestTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ from e in new[]
4545
{
4646
using var ts1 = first.AsTestingSequence();
4747
using var ts2 = second.AsTestingSequence();
48-
return ts1.ZipLongest(ts2, Tuple.Create).ToArray();
48+
return [..ts1.ZipLongest(ts2, Tuple.Create)];
4949
}
5050

5151
[Test]

MoreLinq/EndsWith.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static bool EndsWith<T>(this IEnumerable<T> first, IEnumerable<T> second,
7575
return second.TryAsCollectionLike() is { Count: var secondCount }
7676
? first.TryAsCollectionLike() is not { Count: var firstCount } || secondCount <= firstCount
7777
&& EndsWith(second, secondCount)
78-
: EndsWith(secondList = second.ToList(), secondList.Count);
78+
: EndsWith(secondList = [..second], secondList.Count);
7979

8080
bool EndsWith(IEnumerable<T> second, int count) =>
8181
first.TakeLast(count).SequenceEqual(second, comparer);

MoreLinq/Experimental/Memoize.cs

+2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ public static IEnumerable<T> Memoize<T>(this IEnumerable<T> source) =>
5858
ICollection<T> => source,
5959
IReadOnlyCollection<T> => source,
6060
MemoizedEnumerable<T> => source,
61+
#pragma warning disable IDE0306 // Simplify collection initialization (would change semantics)
6162
_ => new MemoizedEnumerable<T>(source),
63+
#pragma warning restore IDE0306 // Simplify collection initialization
6264
};
6365
}
6466

MoreLinq/Rank.cs

+2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public static IEnumerable<int> RankBy<TSource, TKey>(this IEnumerable<TSource> s
8181

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

8688
var rankDictionary = new Collections.Dictionary<TSource, int>(EqualityComparer<TSource>.Default);
8789
var i = 1;

MoreLinq/SortedMerge.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static IEnumerable<TSource> Impl(IEnumerable<IEnumerable<TSource>> sequences, Fu
158158

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

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

MoreLinq/ToDataTable.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ static MemberInfo[] PrepareMemberInfos<T>(ICollection<Expression<Func<T, object?
171171
throw new ArgumentException("One of the supplied expressions was null.", nameof(expressions));
172172
try
173173
{
174-
return expressions.Select(GetAccessedMember).ToArray();
174+
return [..from e in expressions select GetAccessedMember(e)];
175175
}
176176
catch (ArgumentException e)
177177
{

bld/ExtensionsGenerator/Program.cs

+6-8
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ from e in ms.Select((m, i) => (SourceOrder: i + 1, Method: m))
155155
e.Method.Name,
156156
e.Method.TypeParameterCount,
157157
e.Method.ParameterCount,
158-
new TupleTypeKey(ImmutableList.CreateRange(e.Method.SortableParameterTypes))
158+
new TupleTypeKey([..e.Method.SortableParameterTypes])
159159
select new
160160
{
161161
e.Method,
162162
e.SourceOrder,
163163
};
164164

165-
q = q.ToArray();
165+
q = [..q];
166166

167167
if (args.OptDebug)
168168
{
@@ -336,14 +336,12 @@ static TypeKey CreateTypeKey(TypeSyntax root, Func<string, TypeKey?> abbreviator
336336
?? new SimpleTypeKey(ins.ToString()),
337337
GenericNameSyntax gns =>
338338
new GenericTypeKey(gns.Identifier.ToString(),
339-
ImmutableList.CreateRange(gns.TypeArgumentList.Arguments.Select(Walk))),
339+
[..from arg in gns.TypeArgumentList.Arguments select Walk(arg)]),
340340
ArrayTypeSyntax ats =>
341341
new ArrayTypeKey(Walk(ats.ElementType),
342-
ImmutableList.CreateRange(from rs in ats.RankSpecifiers
343-
select rs.Rank)),
342+
[..from rs in ats.RankSpecifiers select rs.Rank]),
344343
TupleTypeSyntax tts =>
345-
new TupleTypeKey(ImmutableList.CreateRange(from te in tts.Elements
346-
select Walk(te.Type))),
344+
new TupleTypeKey([..from te in tts.Elements select Walk(te.Type)]),
347345
_ => throw new NotSupportedException("Unhandled type: " + ts)
348346
};
349347
}
@@ -415,7 +413,7 @@ public override string ToString() =>
415413
sealed class ArrayTypeKey(TypeKey element, IEnumerable<int> ranks) :
416414
ParameterizedTypeKey("[]", element)
417415
{
418-
public ImmutableList<int> Ranks { get; } = ImmutableList.CreateRange(ranks);
416+
public ImmutableList<int> Ranks { get; } = [..ranks];
419417

420418
public override string ToString() =>
421419
Parameters.Single() + string.Concat(from r in Ranks

0 commit comments

Comments
 (0)