示例#1
0
            static async ValueTask ExecuteAsync(TEnumerable source, AsyncAction <TSource> actionAsync, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                await using (enumerator.ConfigureAwait(false))
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        await actionAsync(enumerator.Current, cancellationToken).ConfigureAwait(false);
                    }
                }
            }
示例#2
0
            static async ValueTask <Option <TSource> > ExecuteAsync(TEnumerable source, int index, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                await using (enumerator.ConfigureAwait(false))
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        if (index-- == 0)
                        {
                            return(Option.Some(enumerator.Current));
                        }
                    }
                }
                return(Option.None);
            }
            static async ValueTask <bool> ComparerContainsAsync(TEnumerable source, [AllowNull] TSource value, IEqualityComparer <TSource> comparer, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                await using (enumerator.ConfigureAwait(false))
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        if (comparer.Equals(enumerator.Current, value !))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
示例#4
0
            static async ValueTask <bool> ExecuteAsync(TEnumerable source, AsyncPredicate <TSource> predicate, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                await using (enumerator.ConfigureAwait(false))
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        if (!await predicate(enumerator.Current, cancellationToken).ConfigureAwait(false))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
            }
            static async ValueTask <Dictionary <TKey, TSource> > ExecuteAsync(TEnumerable source, AsyncSelector <TSource, TKey> keySelector, IEqualityComparer <TKey>?comparer, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                await using (enumerator.ConfigureAwait(false))
                {
                    var dictionary = new Dictionary <TKey, TSource>(0, comparer);
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        var item = enumerator.Current;
                        dictionary.Add(
                            await keySelector(item, cancellationToken).ConfigureAwait(false),
                            item);
                    }
                    return(dictionary);
                }
            }
示例#6
0
            static async ValueTask <bool> DefaultContainsAsync(TEnumerable source, TSource value, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                try
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        if (EqualityComparer <TSource> .Default.Equals(enumerator.Current, value))
                        {
                            return(true);
                        }
                    }
                }
                finally
                {
                    await enumerator.DisposeAsync().ConfigureAwait(false);
                }
                return(false);
            }
            static async ValueTask <bool> ExecuteAsync(TEnumerable source, TPredicate predicate, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                try
                {
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        if (await predicate.InvokeAsync(enumerator.Current, cancellationToken).ConfigureAwait(false))
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
                finally
                {
                    await enumerator.DisposeAsync().ConfigureAwait(false);
                }
            }
示例#8
0
            static async ValueTask <Dictionary <TKey, TSource> > ExecuteAsync(TEnumerable source, TKeySelector keySelector, IEqualityComparer <TKey>?comparer, CancellationToken cancellationToken)
            {
                var enumerator = source.GetAsyncEnumerator(cancellationToken);

                try
                {
                    // ReSharper disable once HeapView.ObjectAllocation.Evident
                    var dictionary = new Dictionary <TKey, TSource>(0, comparer);
                    while (await enumerator.MoveNextAsync().ConfigureAwait(false))
                    {
                        var item = enumerator.Current;
                        dictionary.Add(
                            await keySelector.InvokeAsync(item, cancellationToken).ConfigureAwait(false),
                            item);
                    }

                    return(dictionary);
                }
                finally
                {
                    await enumerator.DisposeAsync().ConfigureAwait(false);
                }
            }