Average.Generated.tt 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <#@ template debug="false" hostspecific="false" language="C#" #>
  2. <#@ assembly name="System.Core" #>
  3. <#@ import namespace="System.Linq" #>
  4. <#@ import namespace="System.Text" #>
  5. <#@ import namespace="System.Collections.Generic" #>
  6. <#@ output extension=".cs" #>
  7. // Licensed to the .NET Foundation under one or more agreements.
  8. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  9. // See the LICENSE file in the project root for more information.
  10. using System.Collections.Generic;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace System.Linq
  14. {
  15. public static partial class AsyncEnumerable
  16. {
  17. <#
  18. var os = new[]
  19. {
  20. new { type = "int", res = "double" },
  21. new { type = "long", res = "double" },
  22. new { type = "float", res = "float" },
  23. new { type = "double", res = "double" },
  24. new { type = "decimal", res = "decimal" },
  25. new { type = "int?", res = "double?" },
  26. new { type = "long?", res = "double?" },
  27. new { type = "float?", res = "float?" },
  28. new { type = "double?", res = "double?" },
  29. new { type = "decimal?", res = "decimal?" },
  30. };
  31. foreach (var o in os)
  32. {
  33. var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
  34. #>
  35. public static Task<<#=o.res#>> AverageAsync(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken = default)
  36. {
  37. if (source == null)
  38. throw Error.ArgumentNull(nameof(source));
  39. return AverageCore(source, cancellationToken);
  40. }
  41. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken = default)
  42. {
  43. if (source == null)
  44. throw Error.ArgumentNull(nameof(source));
  45. if (selector == null)
  46. throw Error.ArgumentNull(nameof(selector));
  47. return AverageCore(source, selector, cancellationToken);
  48. }
  49. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  50. {
  51. if (source == null)
  52. throw Error.ArgumentNull(nameof(source));
  53. if (selector == null)
  54. throw Error.ArgumentNull(nameof(selector));
  55. return AverageCore(source, selector, cancellationToken);
  56. }
  57. #if !NO_DEEP_CANCELLATION
  58. public static Task<<#=o.res#>> AverageAsync<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<<#=o.type#>>> selector, CancellationToken cancellationToken = default)
  59. {
  60. if (source == null)
  61. throw Error.ArgumentNull(nameof(source));
  62. if (selector == null)
  63. throw Error.ArgumentNull(nameof(selector));
  64. return AverageCore(source, selector, cancellationToken);
  65. }
  66. #endif
  67. <#
  68. }
  69. #>
  70. }
  71. }