DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
266 stars 15 forks source link

Add a new `Indexed` method to IEnumerable<T> that returns IEnumerable<(int index, T item)> #11

Closed DavidArno closed 7 years ago

DavidArno commented 7 years ago

For C# 7, as it'll use the new tuple syntax, or add earlier and make a breaking-change conversion to the tuple syntax in v3.

Add a new method to IEnumerable<T>:

public IEnumerable<(int index, T item)> Indexed(this IEnumerable<T> enumeration);

This can then be used in situations where the index, as well as the value, is needed when enumerating a collection:

for (var (index, item) in someCollection.Indexed())
{
    Console.WriteLIne($"Item {index}: {item}");
}
DavidArno commented 7 years ago

Included in the v3.0.0 release.