mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 13:48:47 +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:
parent
847af30945
commit
0ae377250c
@ -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 `Span<T>.Replace(T, T)`.
|
||||
- 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(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
|
@ -37,6 +37,31 @@ public class EnumerableTests
|
||||
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]
|
||||
public void MinMax_ShouldReturnCorrectValues_UsingDefaultComparer()
|
||||
{
|
||||
|
@ -39,6 +39,31 @@ public static class EnumerableExtensions
|
||||
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>
|
||||
/// Returns the minimum and maximum values in a sequence of values.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user