1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 01:18:46 +00:00

feat: add IEnumerable<T>.Except(T)

LINQ-provided Except method filters by array, but there's no way to filter a single value. This method introduces single-value filtering.
This commit is contained in:
Oliver Booth 2023-04-13 20:55:24 +01:00
parent 847af30945
commit 0ae377250c
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 52 additions and 1 deletions

View File

@ -13,7 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added math-related extension methods for `BigInteger`. - X10D: Added math-related extension methods for `BigInteger`.
- X10D: Added `Span<T>.Replace(T, T)`. - X10D: Added `Span<T>.Replace(T, T)`.
- X10D: Added `CountDigits` for integer types. - X10D: Added `CountDigits` for integer types.
- X10D: Added `Progress<T>.OnProgressChanged([T])`; - X10D: Added `IEnumerable<T>.Except(T)`.
- X10D: Added `Progress<T>.OnProgressChanged([T])`.
- X10D: Added `TextWriter.WriteNoAlloc(int[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteNoAlloc(int[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D: Added `TextWriter.WriteNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D: Added `TextWriter.WriteNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.

View File

@ -37,6 +37,31 @@ public class EnumerableTests
Assert.Throws<ArgumentNullException>(() => source!.ConcatOne("Foobar").ToArray()); Assert.Throws<ArgumentNullException>(() => source!.ConcatOne("Foobar").ToArray());
} }
[Test]
public void Except_ShouldFilterElements_GivenMatchingElements()
{
int[] source = Enumerable.Range(1, 10).ToArray();
int[] result = source.Except(5).ToArray();
Assert.That(result, Is.EquivalentTo(new[] {1, 2, 3, 4, 6, 7, 8, 9, 10}));
}
[Test]
public void Except_ShouldReturnSameElements_GivenNoMatchingElements()
{
int[] source = Enumerable.Range(1, 10).ToArray();
int[] result = source.Except(11).ToArray();
Assert.That(result, Is.EquivalentTo(source));
}
[Test]
public void Except_ShouldThrowArgumentNullException_GivenNullSource()
{
IEnumerable<int> source = null!;
Assert.Throws<ArgumentNullException>(() => source.Except(42));
}
[Test] [Test]
public void MinMax_ShouldReturnCorrectValues_UsingDefaultComparer() public void MinMax_ShouldReturnCorrectValues_UsingDefaultComparer()
{ {

View File

@ -39,6 +39,31 @@ public static class EnumerableExtensions
yield return value; yield return value;
} }
/// <summary>
/// Filters a sequence of values by omitting elements that match a specified value.
/// </summary>
/// <param name="source">An <see cref="IEnumerable{T}" /> to filter.</param>
/// <param name="item">The value to omit.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that contains elements from the input sequence that do not match the specified
/// value.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> source, TSource item)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Where(i => !Equals(i, item));
}
/// <summary> /// <summary>
/// Returns the minimum and maximum values in a sequence of values. /// Returns the minimum and maximum values in a sequence of values.
/// </summary> /// </summary>