-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Index.cs
37 lines (35 loc) · 1.64 KB
/
Index.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace SuperLinq.Async;
public static partial class AsyncSuperEnumerable
{
/// <summary>
/// Returns a sequence of tuples where the `key` is
/// the zero-based index of the `value` in the source
/// sequence.
/// </summary>
/// <typeparam name="TSource">Type of elements in <paramref name="source"/> sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <returns>A sequence of tuples.</returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<(int index, TSource item)> Index<TSource>(this IAsyncEnumerable<TSource> source)
{
return source.Index(0);
}
/// <summary>
/// Returns a sequence of tuples where the `key` is
/// the zero-based index of the `value` in the source
/// sequence. An additional parameter specifies the
/// starting index.
/// </summary>
/// <typeparam name="TSource">Type of elements in <paramref name="source"/> sequence.</typeparam>
/// <param name="source">The source sequence.</param>
/// <param name="startIndex"></param>
/// <returns>A sequence of tuples.</returns>
/// <remarks>This operator uses deferred execution and streams its results.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
public static IAsyncEnumerable<(int index, TSource item)> Index<TSource>(this IAsyncEnumerable<TSource> source, int startIndex)
{
ArgumentNullException.ThrowIfNull(source);
return source.Select((item, index) => (startIndex + index, item));
}
}