-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
TakeEvery.cs
32 lines (31 loc) · 1.26 KB
/
TakeEvery.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
namespace SuperLinq.Async;
public static partial class AsyncSuperEnumerable
{
/// <summary>
/// Returns every N-th element of a sequence.
/// </summary>
/// <typeparam name="TSource">Type of the source sequence</typeparam>
/// <param name="source">Source sequence</param>
/// <param name="step">Number of elements to bypass before returning the next element.</param>
/// <returns>
/// A sequence with every N-th element of the input sequence.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="step"/> is negative.</exception>
/// <remarks>
/// This operator uses deferred execution and streams its results.
/// </remarks>
/// <example>
/// <code><![CDATA[
/// int[] numbers = { 1, 2, 3, 4, 5 };
/// var result = numbers.TakeEvery(2);
/// ]]></code>
/// The <c>result</c> variable, when iterated over, will yield 1, 3 and 5, in turn.
/// </example>
public static IAsyncEnumerable<TSource> TakeEvery<TSource>(this IAsyncEnumerable<TSource> source, int step)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(step);
return source.Where((e, i) => i % step == 0);
}
}