Skip to content

Commit

Permalink
Add Index(...) LINQ method (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored Sep 3, 2024
1 parent bfa24b2 commit 07041cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
13 changes: 13 additions & 0 deletions PolyShim.Tests/Net90/EnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ namespace PolyShim.Tests.Net90;

public class EnumerableExtensionsTests
{
[Fact]
public void Index_Test()
{
// Arrange
var source = new[] { 42, 13, 69, 17 };

// Act
var result = source.Index();

// Assert
result.Should().Equal((0, 42), (1, 13), (2, 69), (3, 17));
}

[Fact]
public void CountBy_Test()
{
Expand Down
4 changes: 4 additions & 0 deletions PolyShim/Net90/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace System.Linq;

internal static partial class PolyfillExtensions
{
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.index
public static IEnumerable<(int index, T value)> Index<T>(this IEnumerable<T> source) =>
source.Select((value, index) => (index, value));

// https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.countby
public static IEnumerable<KeyValuePair<TKey, int>> CountBy<TSource, TKey>(
this IEnumerable<TSource> source,
Expand Down

0 comments on commit 07041cc

Please sign in to comment.