Exemple #1
0
        private static async Task <T> FirstAsync <T>(IAsyncEnumerable <T> source, Boolean throwIfNone)
        {
            T   retVal;
            var enumerator = source.GetAsyncEnumerator();

            try
            {
                var success = await enumerator.WaitForNextAsync();

                retVal = success ? enumerator.TryGetNext(out success) : default;
                if (!success)
                {
                    if (throwIfNone)
                    {
                        throw AsyncProviderUtilities.EmptySequenceException();
                    }
                    else
                    {
                        retVal = default;
                    }
                }
            }
            finally
            {
                await enumerator.DisposeAsync();
            }

            return(retVal);
        }
Exemple #2
0
        /// <summary>
        /// This extension method will return <see cref="IAsyncEnumerable{T}"/> which will return only those items which are of given type.
        /// </summary>
        /// <typeparam name="T">The type of source enumerable items.</typeparam>
        /// <typeparam name="U">The type of target items.</typeparam>
        /// <param name="enumerable">This <see cref="IAsyncEnumerable{T}"/>.</param>
        /// <returns><see cref="IAsyncEnumerable{T}"/> which will return only those items which are of given type.</returns>
        /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception>
        /// <seealso cref="System.Linq.Enumerable.OfType{TResult}(System.Collections.IEnumerable)"/>
        public IAsyncEnumerable <U> OfType <T, U>(IAsyncEnumerable <T> enumerable)
        {
            ArgumentValidator.ValidateNotNullReference(enumerable);
            return(AsyncProviderUtilities.IsOfType(
                       typeof(T)
#if !NET40
                       .GetTypeInfo()
#endif
                       , typeof(U)
#if !NET40
                       .GetTypeInfo()
#endif
                       ) ?
                   (IAsyncEnumerable <U>)enumerable :
                   FromTransformCallback(enumerable, e => new OfTypeEnumerator <T, U>(e)));
        }
Exemple #3
0
        /// <summary>
        /// Similarly to <see cref="System.Linq.Enumerable.Aggregate{TSource}(IEnumerable{TSource}, Func{TSource, TSource, TSource})"/> method, this method potentially asynchronously enumerates this <see cref="IAsyncEnumerable{T}"/> and aggregates a single value using the given <paramref name="asyncFunc"/> potentially asynchronous callback.
        /// </summary>
        /// <typeparam name="T">The type of elements being enumerated.</typeparam>
        /// <param name="source">This <see cref="IAsyncEnumerable{T}"/>.</param>
        /// <param name="asyncFunc">The potentially asynchronous callback function to perform aggregation. First argument is previous element, second argument is current element, and return value is the new aggregated value.</param>
        /// <returns>An aggregated value.</returns>
        /// <exception cref="NullReferenceException">If this <see cref="IAsyncEnumerable{T}"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="asyncFunc"/> is <c>null</c>.</exception>
        /// <exception cref="InvalidOperationException">If this <see cref="IAsyncEnumerable{T}"/> does not contain at least one element.</exception>
        public async Task <T> AggregateAsync <T>(IAsyncEnumerable <T> source, Func <T, T, ValueTask <T> > asyncFunc)
        {
            ArgumentValidator.ValidateNotNullReference(source);
            ArgumentValidator.ValidateNotNull(nameof(asyncFunc), asyncFunc);

            var state = INITIAL;
            T   prev  = default;
            await source.EnumerateAsync(async item =>
            {
                if (state == INITIAL)
                {
                    prev = item;
                    Interlocked.Exchange(ref state, FIRST_SEEN);
                }
                else
                {
                    prev = await asyncFunc(prev, item);
                }
            });

            return(state == FIRST_SEEN ? prev : throw AsyncProviderUtilities.EmptySequenceException());
        }
Exemple #4
0
 public Task <T> AggregateAsync <T>(IAsyncEnumerable <T> source, Func <T, T, ValueTask <T> > asyncFunc)
 => throw AsyncProviderUtilities.EmptySequenceException();