Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public async Task Scan1Async()
var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan(8, (x, y) => x + y);

var e = xs.GetAsyncEnumerator();
await HasNextAsync(e, 8);
await HasNextAsync(e, 9);
await HasNextAsync(e, 11);
await HasNextAsync(e, 14);
Expand All @@ -40,6 +41,7 @@ public async Task Scan2Async()
var xs = new[] { 1, 2, 3 }.ToAsyncEnumerable().Scan((x, y) => x + y);

var e = xs.GetAsyncEnumerator();
await HasNextAsync(e, 1);
await HasNextAsync(e, 3);
await HasNextAsync(e, 6);
await NoNextAsync(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ 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.

/// <summary>
/// Applies an accumulator function over an async-enumerable sequence and returns each intermediate result.
/// For aggregation behavior with no intermediate results, see <see cref="AsyncEnumerable.AggregateAsync{TSource}"/>.
Expand Down Expand Up @@ -43,6 +39,8 @@ static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Fu

var res = e.Current;

yield return res;

while (await e.MoveNextAsync())
{
res = accumulator(res, e.Current);
Expand Down Expand Up @@ -76,6 +74,8 @@ static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source
{
var res = seed;

yield return res;

await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
res = accumulator(res, item);
Expand Down Expand Up @@ -114,6 +114,8 @@ static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Fu

var res = e.Current;

yield return res;

while (await e.MoveNextAsync())
{
res = await accumulator(res, e.Current).ConfigureAwait(false);
Expand Down Expand Up @@ -153,6 +155,8 @@ static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Fu

var res = e.Current;

yield return res;

while (await e.MoveNextAsync())
{
res = await accumulator(res, e.Current, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -187,6 +191,8 @@ static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source
{
var res = seed;

yield return res;

await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
res = await accumulator(res, item).ConfigureAwait(false);
Expand Down Expand Up @@ -221,6 +227,8 @@ static async IAsyncEnumerable<TAccumulate> Core(IAsyncEnumerable<TSource> source
{
var res = seed;

yield return res;

await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
res = await accumulator(res, item, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void Scan_Arguments()
public void Scan1()
{
var res = Enumerable.Range(0, 5).Scan((n, x) => n + x).ToList();
Assert.True(Enumerable.SequenceEqual(res, new[] { 1, 3, 6, 10 }));
Assert.True(Enumerable.SequenceEqual(res, new[] { 0, 1, 3, 6, 10 }));
}

[Fact]
public void Scan2()
{
var res = Enumerable.Range(0, 5).Scan(10, (n, x) => n - x).ToList();
Assert.True(Enumerable.SequenceEqual(res, new[] { 10, 9, 7, 4, 0 }));
Assert.True(Enumerable.SequenceEqual(res, new[] { 10, 10, 9, 7, 4, 0 }));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private static IEnumerable<TAccumulate> ScanCore<TSource, TAccumulate>(IEnumerab
{
var acc = seed;

yield return acc;

foreach (var item in source)
{
acc = accumulator(acc, item);
Expand All @@ -73,10 +75,11 @@ private static IEnumerable<TSource> ScanCore<TSource>(IEnumerable<TSource> sourc
{
hasSeed = true;
acc = item;
continue;
}

acc = accumulator(acc, item);
else
{
acc = accumulator(acc, item);
}

yield return acc;
}
Expand Down