Skip to content

Commit

Permalink
Add params based Enumerable.Except (#70)
Browse files Browse the repository at this point in the history
* Update PolyfillExtensions_IEnumerable.cs

* Update api_list.include.md

* Update Directory.Build.props
  • Loading branch information
SimonCropp authored Jul 31, 2023
1 parent b802f15 commit 2605a4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
### IEnumerable<TSource>

* `IEnumerable<TSource> Except<TSource>(TSource)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
* `IEnumerable<TSource> Except<TSource>(TSource[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
* `IEnumerable<TSource> Except<TSource>(TSource, IEqualityComparer<TSource>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
* `IEnumerable<TSource> Except<TSource>(IEqualityComparer<TSource>, TSource[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
* `TSource MaxBy<TSource, TKey>(Func<TSource,TKey>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
* `TSource MaxBy<TSource, TKey>(Func<TSource,TKey>, IComparer<TKey>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.maxby?view=net-8.0#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1))))
* `TSource MinBy<TSource, TKey>(Func<TSource,TKey>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.minby#system-linq-enumerable-minby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1))))
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>1.25.0</Version>
<Version>1.26.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
43 changes: 40 additions & 3 deletions src/Polyfill/PolyfillExtensions_IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
static partial class PolyfillExtensions
{
/// <summary>
/// Produces the set difference of two sequences by using the default equality comparer to compare values.
/// 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<TSource>"/> 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.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1)))")]
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> target, TSource item)
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
TSource item)
{
return Except<TSource>(target, item, null);
}
Expand All @@ -27,11 +29,29 @@ public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> tar
/// </summary>
/// <param name="target">An <see cref="IEnumerable<TSource>"/> 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.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
params TSource[] items)
{
return 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<TSource>"/> 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<TSource>"/> 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.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1)))")]
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> target, TSource item, IEqualityComparer<TSource>? comparer)
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
TSource item,
IEqualityComparer<TSource>? comparer)
{
var set = new HashSet<TSource>(comparer);
set.Add(item);
Expand All @@ -44,6 +64,23 @@ public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> tar
}
}

/// <summary>
/// Produces the set difference of two sequences by <paramref name="comparer"/> to compare values.
/// </summary>
/// <param name="target">An <see cref="IEnumerable<TSource>"/> 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.maxby#system-linq-enumerable-maxby-2(system-collections-generic-ienumerable((-0))-system-func((-0-1)))")]
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> target,
IEqualityComparer<TSource> comparer,
params TSource[] items)
{
return target.Except((IEnumerable<TSource>)items, comparer);
}


#if NETSTANDARD || NETCOREAPP || NETFRAMEWORK || NET5_0

/// <summary>
Expand Down

0 comments on commit 2605a4a

Please sign in to comment.