Skip to content
Merged
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 @@ -19,7 +19,6 @@

<ItemGroup>
<ProjectReference Include="..\System.Interactive.Async.Providers\System.Interactive.Async.Providers.csproj" />
<ProjectReference Include="..\System.Linq.Async\System.Linq.Async.csproj" />
</ItemGroup>

<!--
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,22 @@
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Update="System\Linq\Operators\Sum.Generated.cs">
<DependentUpon>Sum.Generated.tt</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="System\Linq\Operators\Average.Generated.tt">
<LastGenOutput>Average.Generated.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
<None Update="System\Linq\Operators\Sum.Generated.tt">
<LastGenOutput>Sum.Generated.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
1,030 changes: 1,030 additions & 0 deletions Ix.NET/Source/System.Interactive.Async/System/Linq/Operators/Sum.Generated.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT 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
{
<#
var os = new[]
{
new { type = "int", zero = "0", @checked = true },
new { type = "long", zero = "0L", @checked = true },
new { type = "float", zero = "0.0f", @checked = false },
new { type = "double", zero = "0.0", @checked = false },
new { type = "decimal", zero = "0m", @checked = false },
new { type = "int?", zero = "0", @checked = true },
new { type = "long?", zero = "0L", @checked = true },
new { type = "float?", zero = "0.0f", @checked = false },
new { type = "double?", zero = "0.0", @checked = false },
new { type = "decimal?", zero = "0m", @checked = false },
};

foreach (var o in os)
{
var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";

var typeStr = o.type;
if (o.type.EndsWith("?")) {
typeStr = "Nullable{" + o.type.Substring(0, 1).ToUpper() + o.type.Substring(1, o.type.Length - 2) + "}";
}
#>
/// <summary>
/// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">A sequence of values that are used to calculate a sum.</param>
/// <param name="selector">A transform function to apply to each element.</param>
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

return Core(source, selector, cancellationToken);

static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
{
var sum = <#=o.zero#>;

await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var value = selector(item);

<#
if (o.@checked)
{
#>
checked
{
sum += value<#=n#>;
}
<#
}
else
{
#>
sum += value<#=n#>;
<#
}
#>
}

return sum;
}
}

/// <summary>
/// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">A sequence of values that are used to calculate a sum.</param>
/// <param name="selector">An asynchronous transform function to apply to each element.</param>
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

return Core(source, selector, cancellationToken);

static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
{
var sum = <#=o.zero#>;

await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var value = await selector(item).ConfigureAwait(false);

<#
if (o.@checked)
{
#>
checked
{
sum += value<#=n#>;
}
<#
}
else
{
#>
sum += value<#=n#>;
<#
}
#>
}

return sum;
}
}

/// <summary>
/// Computes the sum of a sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">A sequence of values that are used to calculate a sum.</param>
/// <param name="selector">An asynchronous, cancellable transform function to apply to each element.</param>
/// <param name="cancellationToken">The optional cancellation token to be used for cancelling the sequence at any time.</param>
/// <returns>An async-enumerable sequence containing a single element with the sum of the values in the source sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
public static ValueTask<<#=o.type#>> SumAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (selector == null)
throw Error.ArgumentNull(nameof(selector));

return Core(source, selector, cancellationToken);

static async ValueTask<<#=o.type#>> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken)
{
var sum = <#=o.zero#>;

await foreach (TSource item in source.WithCancellation(cancellationToken).ConfigureAwait(false))
{
var value = await selector(item, cancellationToken).ConfigureAwait(false);

<#
if (o.@checked)
{
#>
checked
{
sum += value<#=n#>;
}
<#
}
else
{
#>
sum += value<#=n#>;
<#
}
#>
}

return sum;
}
}
<#
}
#>
}
}
5 changes: 3 additions & 2 deletions Ix.NET/Source/System.Linq.Async/System.Linq.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
<!--
The ref assembly defines the public API visible at compile time, so it should not include any methods for which
System.Linq.AsyncEnumerable defines source-compatible replacements, because their continued availability would
cause compile-time ambiguity.
cause compile-time ambiguity. Similarly, methods we have relocated to System.Interactive.Async need to be
hidden here.
We retain the code because we continue to make these methods available in the runtime assembly (in the lib folder)
to provide binary compatibility. This project builds the runtime assembly, so we need to include these methods.
-->
<DefineConstants>$(DefineConstants);INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES</DefineConstants>
<DefineConstants>$(DefineConstants);INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES;INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC</DefineConstants>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace System.Linq
public static partial class AsyncEnumerable
// NB: Synchronous LINQ to Objects doesn't hide the implementation of the source either.
{
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
// Note: this one isn't actually in the System.Linq.AsyncEnumerable package, so we've moved it
// to System.Interactive.Async because that's the home for LINQ-like implementations for
// IAsyncEnumerable<T> that aren't in the runtime libraries.
Expand All @@ -24,6 +24,6 @@ public static partial class AsyncEnumerable
/// <returns>An async-enumerable sequence that hides the identity of the source sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
public static IAsyncEnumerable<TSource> AsAsyncEnumerable<TSource>(this IAsyncEnumerable<TSource> source) => source;
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
#endif // INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ static async ValueTask<double> Core(IAsyncEnumerable<int> source, CancellationTo
}
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="int" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -222,7 +224,9 @@ static async ValueTask<double> Core(IAsyncEnumerable<long> source, CancellationT
}
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="long" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -394,7 +398,9 @@ static async ValueTask<float> Core(IAsyncEnumerable<float> source, CancellationT
}
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="float" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -566,7 +572,9 @@ static async ValueTask<double> Core(IAsyncEnumerable<double> source, Cancellatio
}
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="double" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -738,7 +746,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<decimal> source, Cancellat
}
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="decimal" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -918,7 +928,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSou
return null;
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="Nullable{Int}" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -1122,7 +1134,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSou
return null;
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="Nullable{Long}" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -1326,7 +1340,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSou
return null;
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="Nullable{Float}" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -1530,7 +1546,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSou
return null;
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="Nullable{Double}" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down Expand Up @@ -1734,7 +1752,9 @@ static async ValueTask<decimal> Core(IAsyncEnumerable<TSource> source, Func<TSou
return null;
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="Nullable{Decimal}" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ else
#>
}
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES

#if INCLUDE_RELOCATED_TO_INTERACTIVE_ASYNC
/// <summary>
/// Computes the average of an async-enumerable sequence of <see cref="<#=typeStr#>" /> values that are obtained by invoking a transform function on each element of the input sequence.
/// </summary>
Expand Down
Loading