Skip to content

Commit

Permalink
re-add except
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Mar 30, 2024
1 parent 2604071 commit 813920a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
128 changes: 64 additions & 64 deletions src/Polyfill/Polyfill_IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,70 @@

static partial class Polyfill
{
// /// <summary>
// /// Produces a set items excluding <paramref name="item"/> by using the default equality comparer to compare values.
// /// </summary>
// /// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
// /// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
// /// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
// /// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-8.0#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0)))")]
// public static IEnumerable<TSource> Except<TSource>(
// this IEnumerable<TSource> target,
// TSource item) =>
// Except<TSource>(target, item, null);
//
// /// <summary>
// /// Produces the set difference of two sequences by using the default equality comparer to compare values.
// /// </summary>
// /// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
// /// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
// /// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
// /// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-8.0#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0)))")]
// public static IEnumerable<TSource> Except<TSource>(
// this IEnumerable<TSource> target,
// params TSource[] items) =>
// target.Except((IEnumerable<TSource>)items);
//
// /// <summary>
// /// Produces a set items excluding <paramref name="item"/> by using <paramref name="comparer"/> to compare values.
// /// </summary>
// /// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
// /// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
// /// <param name="comparer">An <see cref="IEqualityComparer&lt;TSource&gt;"/> to compare values.</param>
// /// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
// /// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")]
// public static IEnumerable<TSource> Except<TSource>(
// this IEnumerable<TSource> target,
// TSource item,
// IEqualityComparer<TSource>? comparer)
// {
// var set = new HashSet<TSource>(comparer);
// set.Add(item);
// foreach (TSource element in target)
// {
// if (set.Add(element))
// {
// yield return element;
// }
// }
// }
//
// /// <summary>
// /// Produces the set difference of two sequences by <paramref name="comparer"/> to compare values.
// /// </summary>
// /// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
// /// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
// /// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
// /// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
// [Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")]
// public static IEnumerable<TSource> Except<TSource>(
// this IEnumerable<TSource> target,
// IEqualityComparer<TSource> comparer,
// params TSource[] items) =>
// target.Except((IEnumerable<TSource>)items, comparer);
/// <summary>
/// Produces a set items excluding <paramref name="item"/> by using the default equality comparer to compare values.
/// </summary>
/// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
/// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
/// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-8.0#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
TSource item) =>
Except<TSource>(target, item, null);

/// <summary>
/// Produces the set difference of two sequences by using the default equality comparer to compare values.
/// </summary>
/// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
/// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
/// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-8.0#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
params TSource[] items) =>
target.Except((IEnumerable<TSource>)items);

/// <summary>
/// Produces a set items excluding <paramref name="item"/> by using <paramref name="comparer"/> to compare values.
/// </summary>
/// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
/// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
/// <param name="comparer">An <see cref="IEqualityComparer&lt;TSource&gt;"/> to compare values.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
/// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
TSource item,
IEqualityComparer<TSource>? comparer)
{
var set = new HashSet<TSource>(comparer);
set.Add(item);
foreach (TSource element in target)
{
if (set.Add(element))
{
yield return element;
}
}
}

/// <summary>
/// Produces the set difference of two sequences by <paramref name="comparer"/> to compare values.
/// </summary>
/// <param name="target">An <see cref="IEnumerable&lt;TSource&gt;"/> whose elements that are not equal to <paramref name="item"/> will be returned.</param>
/// <param name="item">An <see cref="TSource"/> that is elements equal it will cause those elements to be removed from the returned sequence.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="target" />.</typeparam>
/// <returns>A sequence that contains the items of <paramref name="target"/> but excluding <paramref name="item"/>.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except#system-linq-enumerable-except-1(system-collections-generic-ienumerable((-0))-system-collections-generic-ienumerable((-0))-system-collections-generic-iequalitycomparer((-0)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
IEqualityComparer<TSource> comparer,
params TSource[] items) =>
target.Except((IEnumerable<TSource>)items, comparer);

#if !NET9_0_OR_GREATER && FeatureValueTuple

Expand Down
12 changes: 6 additions & 6 deletions src/Tests/PolyfillTests_IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public void MaxBy()
Assert.AreEqual(2, enumerable.MaxBy(_ => _));
}

// [Test]
// public void Except()
// {
// var enumerable = new List<int> {1, 2};
// Assert.AreEqual(1, enumerable.Except(2).Single());
// }
[Test]
public void Except()
{
var enumerable = new List<int> {1, 2};
Assert.AreEqual(1, enumerable.Except(2).Single());
}

[Test]
public void MinBy()
Expand Down

0 comments on commit 813920a

Please sign in to comment.