Skip to content

Commit b667720

Browse files
committed
Create helper method for building collections
1 parent c95c29b commit b667720

File tree

7 files changed

+49
-65
lines changed

7 files changed

+49
-65
lines changed

Ix.NET/Source/System.Linq.Async/System/Linq/AsyncEnumerableHelpers.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ internal static async Task<Set<T>> ToSet<T>(IAsyncEnumerable<T> source, IEqualit
113113
return set;
114114
}
115115

116+
internal static async ValueTask<TResult> ToCollection<TSource, TCollection, TResult>(
117+
this IAsyncEnumerable<TSource> source,
118+
TCollection collection,
119+
Func<TCollection, TResult> resultSelector,
120+
CancellationToken cancellationToken)
121+
where TCollection : ICollection<TSource>
122+
{
123+
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
124+
{
125+
collection.Add(item);
126+
}
127+
128+
return resultSelector(collection);
129+
}
130+
116131
internal struct ArrayWithLength<T>
117132
{
118133
public T[] Array;

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToHashSet.cs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using static System.Linq.Async.System.Linq.FunctionalHelpers;
89

910
namespace System.Linq
1011
{
@@ -37,19 +38,10 @@ public static ValueTask<HashSet<TSource>> ToHashSetAsync<TSource>(this IAsyncEnu
3738
if (source == null)
3839
throw Error.ArgumentNull(nameof(source));
3940

40-
return Core(source, comparer, cancellationToken);
41-
42-
static async ValueTask<HashSet<TSource>> Core(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? comparer, CancellationToken cancellationToken)
43-
{
44-
var set = new HashSet<TSource>(comparer);
45-
46-
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
47-
{
48-
set.Add(item);
49-
}
50-
51-
return set;
52-
}
41+
return source.ToCollection(
42+
new HashSet<TSource>(comparer),
43+
Identity,
44+
cancellationToken);
5345
}
5446
}
5547
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToImmutableHashSet.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,10 @@ public static ValueTask<ImmutableHashSet<TSource>> ToImmutableHashSetAsync<TSour
3838
if (source == null)
3939
throw Error.ArgumentNull(nameof(source));
4040

41-
return Core(source, equalityComparer, cancellationToken);
42-
43-
static async ValueTask<ImmutableHashSet<TSource>> Core(IAsyncEnumerable<TSource> source, IEqualityComparer<TSource>? equalityComparer, CancellationToken cancellationToken)
44-
{
45-
var builder = ImmutableHashSet.CreateBuilder(equalityComparer);
46-
47-
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
48-
{
49-
builder.Add(item);
50-
}
51-
52-
return builder.ToImmutable();
53-
}
41+
return source.ToCollection(
42+
ImmutableHashSet.CreateBuilder(equalityComparer),
43+
static builder => builder.ToImmutable(),
44+
cancellationToken);
5445
}
5546
}
5647
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToImmutableList.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,10 @@ public static ValueTask<ImmutableList<TSource>> ToImmutableListAsync<TSource>(th
2525
if (source == null)
2626
throw Error.ArgumentNull(nameof(source));
2727

28-
return Core(source, cancellationToken);
29-
30-
static async ValueTask<ImmutableList<TSource>> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default)
31-
{
32-
var builder = ImmutableList.CreateBuilder<TSource>();
33-
34-
await foreach (var item in source.WithCancellation(cancellationToken))
35-
{
36-
builder.Add(item);
37-
}
38-
39-
return builder.ToImmutable();
40-
}
28+
return source.ToCollection(
29+
ImmutableList.CreateBuilder<TSource>(),
30+
static builder => builder.ToImmutable(),
31+
cancellationToken);
4132
}
4233
}
4334
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToImmutableSortedSet.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,10 @@ public static ValueTask<ImmutableSortedSet<TSource>> ToImmutableSortedSetAsync<T
3838
if (source == null)
3939
throw Error.ArgumentNull(nameof(source));
4040

41-
return Core(source, comparer, cancellationToken);
42-
43-
static async ValueTask<ImmutableSortedSet<TSource>> Core(IAsyncEnumerable<TSource> source, IComparer<TSource>? comparer, CancellationToken cancellationToken)
44-
{
45-
var builder = ImmutableSortedSet.CreateBuilder(comparer);
46-
47-
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
48-
{
49-
builder.Add(item);
50-
}
51-
52-
return builder.ToImmutable();
53-
}
41+
return source.ToCollection(
42+
ImmutableSortedSet.CreateBuilder(comparer),
43+
static builder => builder.ToImmutable(),
44+
cancellationToken);
5445
}
5546
}
5647
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToList.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using static System.Linq.Async.System.Linq.FunctionalHelpers;
89

910
namespace System.Linq
1011
{
@@ -27,19 +28,7 @@ public static ValueTask<List<TSource>> ToListAsync<TSource>(this IAsyncEnumerabl
2728
if (source is IAsyncIListProvider<TSource> listProvider)
2829
return listProvider.ToListAsync(cancellationToken);
2930

30-
return Core(source, cancellationToken);
31-
32-
static async ValueTask<List<TSource>> Core(IAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
33-
{
34-
var list = new List<TSource>();
35-
36-
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
37-
{
38-
list.Add(item);
39-
}
40-
41-
return list;
42-
}
31+
return source.ToCollection(new List<TSource>(), Identity, cancellationToken);
4332
}
4433
}
4534
}

Ix.NET/Source/System.Linq.Async/System/Linq/Operators/Utilities.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,20 @@ public static async ValueTask AddRangeAsync<T>(this List<T> list, IAsyncEnumerab
4343
list.Add(item);
4444
}
4545
}
46+
47+
public static async ValueTask<TResult> ToCollection<TSource, TCollection, TResult>(
48+
this IAsyncEnumerable<TSource> source,
49+
TCollection collection,
50+
Func<TCollection, TResult> resultSelector,
51+
CancellationToken cancellationToken)
52+
where TCollection : ICollection<TSource>
53+
{
54+
await foreach (var item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
55+
{
56+
collection.Add(item);
57+
}
58+
59+
return resultSelector(collection);
60+
}
4661
}
4762
}

0 commit comments

Comments
 (0)