-
Couldn't load subscription status.
- Fork 5.2k
Add {Async}Enumerable.{Infinite}Sequence #116538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,454
−236
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/libraries/System.Linq.AsyncEnumerable/ref/SystemLinq.AsyncEnumerable.netcore.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // ------------------------------------------------------------------------------ | ||
| // Changes to this file must follow the https://aka.ms/api-review process. | ||
| // ------------------------------------------------------------------------------ | ||
|
|
||
| namespace System.Linq | ||
| { | ||
| public static partial class AsyncEnumerable | ||
| { | ||
| public static System.Collections.Generic.IAsyncEnumerable<T> InfiniteSequence<T>(T start, T step) where T : System.Numerics.IAdditionOperators<T, T, T> { throw null; } | ||
| public static System.Collections.Generic.IAsyncEnumerable<T> Sequence<T>(T start, T endInclusive, T step) where T : System.Numerics.INumber<T> { throw null; } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/InfiniteSequence.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.Linq | ||
| { | ||
| public static partial class AsyncEnumerable | ||
| { | ||
| /// <summary>Generates an infinite sequence that begins with <paramref name="start"/> and yields additional values each incremented by <paramref name="step"/>.</summary> | ||
| /// <typeparam name="T">The type of the value to be yielded in the result sequence.</typeparam> | ||
| /// <param name="start">The starting value.</param> | ||
| /// <param name="step">The amount by which the next yielded value should be incremented from the previous yielded value.</param> | ||
| /// <returns>An <see cref="IAsyncEnumerable{T}"/> that contains the sequence.</returns> | ||
| public static IAsyncEnumerable<T> InfiniteSequence<T>(T start, T step) where T : IAdditionOperators<T, T, T> | ||
| { | ||
| if (start is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(start)); | ||
| } | ||
|
|
||
| if (step is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(step)); | ||
| } | ||
|
|
||
| return Iterator(start, step); | ||
|
|
||
| static async IAsyncEnumerable<T> Iterator(T start, T step) | ||
| { | ||
| while (true) | ||
| { | ||
| yield return start; | ||
| start += step; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
src/libraries/System.Linq.AsyncEnumerable/src/System/Linq/Sequence.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace System.Linq | ||
| { | ||
| public static partial class AsyncEnumerable | ||
| { | ||
| /// <summary>Generates a sequence that begins with <paramref name="start"/> and yields additional values each incremented by <paramref name="step"/> until <paramref name="endInclusive"/> is reached.</summary> | ||
| /// <typeparam name="T">The type of the value to be yielded in the result sequence.</typeparam> | ||
| /// <param name="start">The starting value. This value will always be included in the resulting sequence.</param> | ||
| /// <param name="endInclusive">The ending bound beyond which values will not be included in the sequence.</param> | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <param name="step">The amount by which the next value in the sequence should be incremented from the previous value.</param> | ||
| /// <returns>An <see cref="IAsyncEnumerable{T}"/> that contains the sequence.</returns> | ||
| /// <exception cref="ArgumentNullException"><paramref name="start"/> is <see langword="null"/>.</exception> | ||
| /// <exception cref="ArgumentNullException"><paramref name="step"/> is <see langword="null"/>.</exception> | ||
| /// <exception cref="ArgumentNullException"><paramref name="endInclusive"/> is <see langword="null"/>.</exception> | ||
| /// <exception cref="ArgumentOutOfRangeException"><paramref name="step"/> is greater than zero but <paramref name="endInclusive"/> is less than <paramref name="start"/>.</exception> | ||
| /// <exception cref="ArgumentOutOfRangeException"><paramref name="step"/> is less than zero but <paramref name="endInclusive"/> is greater than <paramref name="start"/>.</exception> | ||
| /// <exception cref="ArgumentOutOfRangeException"><paramref name="step"/> is zero and <paramref name="endInclusive"/> does not equal <paramref name="start"/>.</exception> | ||
| public static IAsyncEnumerable<T> Sequence<T>(T start, T endInclusive, T step) where T : INumber<T> | ||
| { | ||
| if (start is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(start)); | ||
| } | ||
|
|
||
| if (endInclusive is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(endInclusive)); | ||
| } | ||
|
|
||
| if (step is null) | ||
| { | ||
| ThrowHelper.ThrowArgumentNullException(nameof(step)); | ||
| } | ||
|
|
||
| if (step > T.Zero) | ||
| { | ||
| // Presumed to be the most common case, step > 0. Validate that endInclusive >= start, as otherwise we can't easily | ||
| // guarantee that the sequence will terminate. | ||
| if (endInclusive < start) | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(nameof(endInclusive)); | ||
| } | ||
|
|
||
| // Otherwise, just produce an incrementing sequence. | ||
| return IncrementingIterator(start, endInclusive, step); | ||
| } | ||
| else if (step < T.Zero) | ||
| { | ||
| // step < 0. Validate that endInclusive <= start, as otherwise we can't easily guarantee that the sequence will terminate. | ||
| if (endInclusive > start) | ||
| { | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(nameof(endInclusive)); | ||
| } | ||
|
|
||
| // Then produce the decrementing sequence. | ||
| return DecrementingIterator(start, endInclusive, step); | ||
| } | ||
| else | ||
| { | ||
| // step == 0. If start != endInclusive, then the sequence would be infinite. As such, we validate | ||
| // that they're equal, and if they are, we return a sequence that yields the start/endInclusive value once. | ||
| if (start != endInclusive) | ||
| { | ||
| ThrowHelper.ThrowArgumentOutOfRangeException(nameof(step)); | ||
| } | ||
|
|
||
| return Repeat(start, 1); | ||
| } | ||
|
|
||
| static async IAsyncEnumerable<T> IncrementingIterator(T start, T endInclusive, T step) | ||
| { | ||
| Debug.Assert(step > T.Zero); | ||
|
|
||
| yield return start; | ||
|
|
||
| while (true) | ||
| { | ||
| T next = start + step; | ||
|
|
||
| if (next >= endInclusive || next <= start) | ||
| { | ||
| if (next == endInclusive && start != next) | ||
| { | ||
| yield return next; | ||
| } | ||
|
|
||
| yield break; | ||
| } | ||
|
|
||
| yield return next; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| start = next; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| static async IAsyncEnumerable<T> DecrementingIterator(T start, T endInclusive, T step) | ||
| { | ||
| Debug.Assert(step < T.Zero); | ||
|
|
||
| yield return start; | ||
|
|
||
| while (true) | ||
| { | ||
| T next = start + step; | ||
|
|
||
| if (next <= endInclusive || next >= start) | ||
| { | ||
| if (next == endInclusive && start != next) | ||
| { | ||
| yield return next; | ||
| } | ||
|
|
||
| yield break; | ||
| } | ||
|
|
||
| yield return next; | ||
| start = next; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.