-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
SkipUntil.cs
63 lines (58 loc) · 2.1 KB
/
SkipUntil.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Skips items from the input sequence until the given predicate returns true when applied to the current
/// source item; that item will be the last skipped.
/// </summary>
/// <typeparam name="TSource">
/// Type of the source sequence
/// </typeparam>
/// <param name="source">
/// Source sequence
/// </param>
/// <param name="predicate">
/// Predicate used to determine when to stop yielding results from the source.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>
/// </exception>
/// <returns>
/// Items from the source sequence after the predicate first returns true when applied to the item.
/// </returns>
/// <remarks>
/// <para>
/// SkipUntil differs from <see cref="Enumerable.SkipWhile{TSource}(IEnumerable{TSource}, Func{TSource,
/// bool})"/> in two respects.
/// </para>
/// <para>
/// Firstly, the sense of the predicate is reversed: it is expected that the predicate will return <see
/// langword="false"/> to start with, and then return <see langword="true"/> - for example, when trying to find
/// a matching item in a sequence.
/// </para>
/// <para>
/// Secondly, SkipUntil skips the element which causes the predicate to return <see langword="true"/>.
/// </para>
/// <para>
/// This operator uses deferred execution and streams its result.
/// </para>
/// </remarks>
public static IEnumerable<TSource> SkipUntil<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(predicate);
return Core(source, predicate);
static IEnumerable<TSource> Core(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
using var enumerator = source.GetEnumerator();
do
{
if (!enumerator.MoveNext())
yield break;
}
while (!predicate(enumerator.Current));
while (enumerator.MoveNext())
yield return enumerator.Current;
}
}
}