Sum.Generated.tt 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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", zero = "0" },
  21. new { type = "long", zero = "0L" },
  22. new { type = "float", zero = "0.0f" },
  23. new { type = "double", zero = "0.0" },
  24. new { type = "decimal", zero = "0m" },
  25. new { type = "int?", zero = "(int?)0" },
  26. new { type = "long?", zero = "(long?)0L" },
  27. new { type = "float?", zero = "(float?)0.0f" },
  28. new { type = "double?", zero = "(double?)0.0" },
  29. new { type = "decimal?", zero = "(decimal?)0m" },
  30. };
  31. foreach (var o in os)
  32. {
  33. var n = o.type.EndsWith("?") ? ".GetValueOrDefault()" : "";
  34. #>
  35. public static Task<<#=o.type#>> Sum(this IAsyncEnumerable<<#=o.type#>> source)
  36. {
  37. if (source == null)
  38. throw new ArgumentNullException(nameof(source));
  39. return source.Aggregate(<#=o.zero#>, (x, y) => x + y<#=n#>, CancellationToken.None);
  40. }
  41. public static Task<<#=o.type#>> Sum(this IAsyncEnumerable<<#=o.type#>> source, CancellationToken cancellationToken)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. return source.Aggregate(<#=o.zero#>, (x, y) => x + y<#=n#>, cancellationToken);
  46. }
  47. public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector)
  48. {
  49. if (source == null)
  50. throw new ArgumentNullException(nameof(source));
  51. if (selector == null)
  52. throw new ArgumentNullException(nameof(selector));
  53. return source.Select(selector).Sum(CancellationToken.None);
  54. }
  55. public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, <#=o.type#>> selector, CancellationToken cancellationToken)
  56. {
  57. if (source == null)
  58. throw new ArgumentNullException(nameof(source));
  59. if (selector == null)
  60. throw new ArgumentNullException(nameof(selector));
  61. return source.Select(selector).Sum(cancellationToken);
  62. }
  63. public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector)
  64. {
  65. if (source == null)
  66. throw new ArgumentNullException(nameof(source));
  67. if (selector == null)
  68. throw new ArgumentNullException(nameof(selector));
  69. return source.Select(selector).Sum(CancellationToken.None);
  70. }
  71. public static Task<<#=o.type#>> Sum<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, Task<<#=o.type#>>> selector, CancellationToken cancellationToken)
  72. {
  73. if (source == null)
  74. throw new ArgumentNullException(nameof(source));
  75. if (selector == null)
  76. throw new ArgumentNullException(nameof(selector));
  77. return source.Select(selector).Sum(cancellationToken);
  78. }
  79. <#
  80. }
  81. #>
  82. }
  83. }