-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8aa403
commit a2ebc49
Showing
16 changed files
with
250 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace System.Linq; | ||
|
||
/// <summary> | ||
/// Enumerable Plus Extensions | ||
/// </summary> | ||
public static partial class EnumerablePlus | ||
{ | ||
/// <summary> | ||
/// Filters an array of values based on a predicate. | ||
/// </summary> | ||
public static TSource[] | ||
WhereArray<TSource>(this TSource[] @this, Func<TSource, bool> predicate) => | ||
Array.FindAll(@this, new Predicate<TSource>(predicate)); | ||
|
||
/// <summary> | ||
/// Sorts the elements of an array in ascending order according to a key. | ||
/// </summary> | ||
public static TSource[] | ||
OrderByArray<TSource, TKey>(this TSource[] @this, Func<TSource, TKey> selector) | ||
where TKey : IComparable<TKey> | ||
{ | ||
var copy = new TSource[@this.Length]; | ||
Array.Copy(@this, copy, @this.Length); | ||
Array.Sort(copy, KeyComparison(selector)); | ||
return copy; | ||
} | ||
|
||
/// <summary> | ||
/// Sorts the elements of an array in ascending order. | ||
/// </summary> | ||
public static TSource[] OrderArray<TSource>(this TSource[] @this) | ||
{ | ||
var copy = new TSource[@this.Length]; | ||
Array.Copy(@this, copy, @this.Length); | ||
Array.Sort(copy); | ||
return copy; | ||
} | ||
|
||
/// <summary> | ||
/// Projects each element of an array into a new form. | ||
/// </summary> | ||
public static TResult[] | ||
SelectArray<TSource, TResult>(this TSource[] @this, Func<TSource, TResult> selector) => | ||
Array.ConvertAll(@this, new Converter<TSource, TResult>(selector)); | ||
|
||
static Comparison<TSource> KeyComparison<TSource, TKey>(Func<TSource, TKey> key) | ||
where TKey : IComparable<TKey> => | ||
(x, y) => Comparer<TKey>.Default.Compare(key(x), key(y)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public static partial class EnumerablePlus | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace CSharpPlus.Tests; | ||
|
||
public class ExpressionExtensionsTests | ||
{ | ||
record ClassWithMembers(int Foo, string Bar); | ||
|
||
[Test] | ||
public void ShouldGetClassMemberName() | ||
{ | ||
Expression<Func<ClassWithMembers, object>> bar = x => x.Bar; | ||
Expression<Func<ClassWithMembers, object>> foo = x => x.Foo; | ||
Expression<Func<ClassWithMembers, object>> length = x => x.Bar.Length; | ||
|
||
bar.GetMemberName().Should().Be(nameof(ClassWithMembers.Bar)); | ||
foo.GetMemberName().Should().Be(nameof(ClassWithMembers.Foo)); | ||
length.GetMemberName().Should().Be(nameof(string.Length)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
namespace CSharpPlus.Tests; | ||
|
||
public class LinqArrayExtensionsTests | ||
{ | ||
[Test] | ||
public void ShouldFilterSampleArray() | ||
{ | ||
int[] items = | ||
{ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, | ||
}; | ||
|
||
int[] expected = | ||
{ | ||
2, 4, 6, 8, 10, 11, 12, 13, | ||
}; | ||
|
||
items.WhereArray(x => x % 2 == 0 || x.ToString().Length is 2) | ||
.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[PropertyTest] | ||
public void ShouldFilterArray(int[] items, Func<int, bool> pred) | ||
{ | ||
var expected = items.Where(pred).ToArray(); | ||
items.WhereArray(pred).Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Test] | ||
public void ShouldMapSampleTest() | ||
{ | ||
int[] items = | ||
{ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
}; | ||
|
||
string[] expected = | ||
{ | ||
"odd[1]", | ||
"even[2]", | ||
"odd[3]", | ||
"even[4]", | ||
"odd[5]", | ||
"even[6]", | ||
"odd[7]", | ||
"even[8]", | ||
"odd[9]", | ||
"even[10]", | ||
}; | ||
|
||
items.SelectArray(x => $"{(x % 2 == 0 ? "even" : "odd")}[{x}]") | ||
.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[PropertyTest] | ||
public void ShouldMapArray(int[] items) | ||
{ | ||
string Selector(int x) => $"{(x % 2 == 0 ? "even" : "odd")}[{x}]"; | ||
var expected = items.Select(Selector).ToArray(); | ||
items.SelectArray(Selector).Should().BeEquivalentTo(expected); | ||
} | ||
|
||
|
||
[Test] | ||
public void ShouldOrderSampleArray() | ||
{ | ||
int[] items = | ||
{ | ||
10, 5, 3, 8, 2, 4, 9, 6, 1, 7, | ||
}; | ||
|
||
int[] expected = | ||
{ | ||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
}; | ||
|
||
items.OrderArray() | ||
.Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[PropertyTest] | ||
public void ShouldOrderArray(int[] items) | ||
{ | ||
var expected = items.OrderBy(x => x).ToArray(); | ||
items.OrderArray().Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[PropertyTest] | ||
public void ShouldOrderByArray(int[] items, Func<int, int> pred) | ||
{ | ||
var expected = items.OrderBy(pred).ToArray(); | ||
items.OrderByArray(pred).Should().BeEquivalentTo(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace CSharpPlus.Tests.Utils; | ||
|
||
public sealed class PropertyTestAttribute : FsCheck.NUnit.PropertyAttribute | ||
{ | ||
public PropertyTestAttribute() => this.QuietOnSuccess = true; | ||
} |