Exemple #1
0
 /// <summary>
 /// Returns <see cref="IAsyncEnumerable{T}"/> that will indefinetly repeat the value returned by given synchronous factory callback.
 /// </summary>
 /// <typeparam name="T">The type of item to repeat.</typeparam>
 /// <param name="generator">A synchronous callback to dynamically generate the value to repeat.</param>
 /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/>.</param>
 /// <returns>An enumerable that will indefinetly repeat the given value.</returns>
 public static IAsyncEnumerable <T> Neverending <T>(
     Func <T> generator,
     IAsyncProvider asyncProvider
     )
 {
     return(AsyncEnumerationFactory.CreateStatelessWrappingEnumerable(AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo(
                                                                          ( out Boolean success ) =>
     {
         success = true;
         return generator();
     }
                                                                          ), asyncProvider));
 }
Exemple #2
0
        /// <summary>
        /// Returns <see cref="IAsyncEnumerable{T}"/> that will return numbers in given range specification, as 64-bit integers.
        /// </summary>
        /// <param name="initial">The start of the range, inclusive.</param>
        /// <param name="target">The end of the range, exclusive.</param>
        /// <param name="asyncProvider">The <see cref="IAsyncProvider"/> for the returned <see cref="IAsyncEnumerable{T}"/>.</param>
        /// <param name="step">The amount to increase for each number within the range. By default, is <c>1</c> for increasing ranges, and <c>-1</c> for decreasing ranges. Specifying invalid values will reset this to default value.</param>
        /// <returns>An enumerable that contains numbers within the given range specification.</returns>
        /// <remarks>
        /// Note that unlike <see cref="System.Linq.Enumerable.Range(Int32, Int32)"/>, this method has exclusive _maximum_ amount as second parameter, instead of amount of values to generate.
        /// This method also handles both increasing and decreasing number ranges.
        /// </remarks>
        /// <seealso cref="System.Linq.Enumerable.Range(Int32, Int32)"/>
        public static IAsyncEnumerable <Int64> Range(
            Int64 initial,
            Int64 target,
            IAsyncProvider asyncProvider,
            Int64 step = 1
            )
        {
            IAsyncEnumerable <Int64> retVal;

            if (target > initial)
            {
                // Increasing range from initial to target, step must be 1 or greater
                step   = Math.Max(1, step);
                retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable(() =>
                {
                    var current = initial;
                    return(AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo(
                               ( out Boolean success ) =>
                    {
                        var prev = current;
                        success = current < target;
                        current += step;
                        return prev;
                    }));
                }, asyncProvider);
            }
            else if (initial == target)
            {
                // Empty
                retVal = EmptyAsync <Int64> .Enumerable;
            }
            else
            {
                // Decreasing range from target to initial, step must be -1 or less
                step   = Math.Min(-1, step);
                retVal = AsyncEnumerationFactory.CreateStatefulWrappingEnumerable(() =>
                {
                    var current = initial;
                    return(AsyncEnumerationFactory.CreateSynchronousWrappingStartInfo(
                               ( out Boolean success ) =>
                    {
                        var prev = current;
                        success = current > target;
                        current -= step;
                        return prev;
                    }));
                }, asyncProvider);
            }

            return(retVal);
        }
Exemple #3
0
        public IAsyncEnumerator <T> GetAsyncEnumerator()
        {
            var startInfo = this._enumerationStart();

            return(AsyncEnumerationFactory.CreateSequentialEnumerator(startInfo.MoveNext, startInfo.Dispose));
        }