Generate.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. namespace System.Linq
  7. {
  8. public static partial class AsyncEnumerableEx
  9. {
  10. // REVIEW: Add async variant?
  11. public static IAsyncEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  12. {
  13. if (condition == null)
  14. throw Error.ArgumentNull(nameof(condition));
  15. if (iterate == null)
  16. throw Error.ArgumentNull(nameof(iterate));
  17. if (resultSelector == null)
  18. throw Error.ArgumentNull(nameof(resultSelector));
  19. return AsyncEnumerable.Create(Core);
  20. #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
  21. async IAsyncEnumerator<TResult> Core(CancellationToken cancellationToken)
  22. {
  23. for (var state = initialState; condition(state); state = iterate(state))
  24. {
  25. // REVIEW: Check for cancellation?
  26. yield return resultSelector(state);
  27. }
  28. }
  29. #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
  30. }
  31. }
  32. }