123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerableEx
- {
- // NB: Implementations of Scan never yield the first element, unlike the behavior of Aggregate on a sequence with one
- // element, which returns the first element (or the seed if given an empty sequence). This is compatible with Rx
- // but one could argue whether it was the right default.
- public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, TSource> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
- {
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- yield break;
- }
- var res = e.Current;
- while (await e.MoveNextAsync())
- {
- res = accumulator(res, e.Current);
- yield return res;
- }
- }
- }
- }
- public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
- {
- var res = seed;
- await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
- {
- res = accumulator(res, item);
- yield return res;
- }
- }
- }
- public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
- {
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- yield break;
- }
- var res = e.Current;
- while (await e.MoveNextAsync())
- {
- res = await accumulator(res, e.Current).ConfigureAwait(false);
- yield return res;
- }
- }
- }
- }
- #if !NO_DEEP_CANCELLATION
- public static IAsyncEnumerable<TSource> Scan<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, TSource, CancellationToken, ValueTask<TSource>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
- {
- await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
- {
- if (!await e.MoveNextAsync())
- {
- yield break;
- }
- var res = e.Current;
- while (await e.MoveNextAsync())
- {
- res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
- yield return res;
- }
- }
- }
- }
- #endif
- public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, ValueTask<TAccumulate>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
- {
- var res = seed;
- await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
- {
- res = await accumulator(res, item).ConfigureAwait(false);
- yield return res;
- }
- }
- }
- #if !NO_DEEP_CANCELLATION
- public static IAsyncEnumerable<TAccumulate> Scan<TSource, TAccumulate>(this IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, CancellationToken, ValueTask<TAccumulate>> accumulator)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (accumulator == null)
- throw Error.ArgumentNull(nameof(accumulator));
- return AsyncEnumerable.Create(Core);
- async IAsyncEnumerator<TAccumulate> Core(CancellationToken cancellationToken)
- {
- var res = seed;
- await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
- {
- res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
- yield return res;
- }
- }
- }
- #endif
- }
- }
|