PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/source/Bindable.Linq/Extensions/Aggregators/BindableEnumerable.Aggregate.cs

#
C# | 144 lines | 67 code | 9 blank | 68 comment | 4 complexity | 3d201693945dd42abfe231d88d58555c MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. using System;
  2. using System.Linq.Expressions;
  3. using Bindable.Linq.Aggregators;
  4. using Bindable.Linq.Helpers;
  5. using Bindable.Linq.Interfaces;
  6. using Bindable.Linq.Dependencies;
  7. namespace Bindable.Linq
  8. {
  9. public static partial class BindableEnumerable
  10. {
  11. /// <summary>
  12. /// Applies an accumulator function over a sequence.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
  15. /// <typeparam name="TResult">The type of the result.</typeparam>
  16. /// <param name="source">An <see cref="IBindableCollection{TSource}"/> to aggregate over.</param>
  17. /// <param name="func">An accumulator function to be invoked on each element.</param>
  18. /// <returns>The final accumulator value.</returns>
  19. public static IBindable<TResult> Aggregate<TSource, TResult>(this IBindableCollection<TSource> source, Expression<Func<IBindableCollection<TSource>, TResult>> func)
  20. {
  21. return source.Aggregate(func, DefaultDependencyAnalysis);
  22. }
  23. /// <summary>
  24. /// Applies an accumulator function over a sequence.
  25. /// </summary>
  26. /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
  27. /// <param name="source">An <see cref="IBindableCollection{TSource}"/> to aggregate over.</param>
  28. /// <param name="func">An accumulator function to be invoked on each element.</param>
  29. /// <returns>The final accumulator value.</returns>
  30. public static IBindable<TSource> Aggregate<TSource>(this IBindableCollection<TSource> source, Expression<Func<IBindableCollection<TSource>, TSource>> func)
  31. {
  32. return source.Aggregate(func, DefaultDependencyAnalysis);
  33. }
  34. /// <summary>
  35. /// Applies an accumulator function over a sequence. The specified seed value is used as the
  36. /// initial accumulator value.
  37. /// </summary>
  38. /// <param name="source">An <see cref="IBindableCollection{TElement}" /> to aggregate over.</param>
  39. /// <param name="seed">The initial accumulator value.</param>
  40. /// <param name="func">An accumulator function to be invoked on each element.</param>
  41. /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
  42. /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
  43. /// <returns>The final accumulator value.</returns>
  44. public static IBindable<TAccumulate> Aggregate<TSource, TAccumulate>(this IBindableCollection<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func)
  45. {
  46. return source.Aggregate(seed, func, DefaultDependencyAnalysis);
  47. }
  48. /// <summary>
  49. /// Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
  50. /// </summary>
  51. /// <param name="source">An <see cref="IBindableCollection{TElement}" /> to aggregate over.</param>
  52. /// <param name="seed">The initial accumulator value.</param>
  53. /// <param name="func">An accumulator function to be invoked on each element.</param>
  54. /// <param name="resultSelector">A function to transform the final accumulator value into the result value.</param>
  55. /// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
  56. /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
  57. /// <typeparam name="TResult">The type of the resulting value.</typeparam>
  58. /// <returns>The transformed final accumulator value.</returns>
  59. public static IBindable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IBindableCollection<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> resultSelector)
  60. {
  61. return source.Aggregate(seed, func, resultSelector, DefaultDependencyAnalysis);
  62. }
  63. /// <summary>
  64. /// Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
  65. /// </summary>
  66. /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
  67. /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
  68. /// <typeparam name="TResult">The type of the resulting value.</typeparam>
  69. /// <param name="source">An <see cref="IBindableCollection{TElement}"/> to aggregate over.</param>
  70. /// <param name="seed">The initial accumulator value.</param>
  71. /// <param name="func">An accumulator function to be invoked on each element.</param>
  72. /// <param name="resultSelector">A function to transform the final accumulator value into the result value.</param>
  73. /// <param name="dependencyAnalysisMode">The dependency analysis mode.</param>
  74. /// <returns>The transformed final accumulator value.</returns>
  75. public static IBindable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IBindableCollection<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, Expression<Func<TAccumulate, TResult>> resultSelector, DependencyDiscovery dependencyAnalysisMode)
  76. {
  77. return source.Aggregate(seed, func, dependencyAnalysisMode).Project(resultSelector, dependencyAnalysisMode);
  78. }
  79. /// <summary>
  80. /// Applies an accumulator function over a sequence.
  81. /// </summary>
  82. /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
  83. /// <typeparam name="TResult">The type of the result.</typeparam>
  84. /// <param name="source">An <see cref="IBindableCollection{TSource}"/> to aggregate over.</param>
  85. /// <param name="func">An accumulator function to be invoked on each element.</param>
  86. /// <param name="dependencyAnalysisMode">The dependency analysis mode.</param>
  87. /// <returns>The final accumulator value.</returns>
  88. public static IBindable<TResult> Aggregate<TSource, TResult>(this IBindableCollection<TSource> source, Expression<Func<IBindableCollection<TSource>, TResult>> func, DependencyDiscovery dependencyAnalysisMode)
  89. {
  90. source.ShouldNotBeNull("source");
  91. func.ShouldNotBeNull("func");
  92. var result = new CustomAggregator<TSource, TResult>(source, func.Compile(), source.Dispatcher);
  93. if (dependencyAnalysisMode == DependencyDiscovery.Enabled)
  94. {
  95. return result.DependsOnExpression(func, func.Parameters[0]);
  96. }
  97. return result;
  98. }
  99. /// <summary>
  100. /// Applies an accumulator function over a sequence. The specified seed value is used as the
  101. /// initial accumulator value.
  102. /// </summary>
  103. /// <typeparam name="TSource">The type of the elements of <paramref name="source"/>.</typeparam>
  104. /// <typeparam name="TAccumulate">The type of the accumulator value.</typeparam>
  105. /// <param name="source">An <see cref="IBindableCollection{TElement}"/> to aggregate over.</param>
  106. /// <param name="seed">The initial accumulator value.</param>
  107. /// <param name="func">An accumulator function to be invoked on each element.</param>
  108. /// <param name="dependencyAnalysisMode">The dependency analysis mode.</param>
  109. /// <returns>The final accumulator value.</returns>
  110. public static IBindable<TAccumulate> Aggregate<TSource, TAccumulate>(this IBindableCollection<TSource> source, TAccumulate seed, Expression<Func<TAccumulate, TSource, TAccumulate>> func, DependencyDiscovery dependencyAnalysisMode)
  111. {
  112. source.ShouldNotBeNull("source");
  113. func.ShouldNotBeNull("func");
  114. seed.ShouldNotBeNull("seed");
  115. var compiledAccumulator = func.Compile();
  116. var function = new Func<IBindableCollection<TSource>, TAccumulate>(
  117. sourceElements =>
  118. {
  119. var current = seed;
  120. foreach (var sourceElement in sourceElements)
  121. {
  122. current = compiledAccumulator(current, sourceElement);
  123. }
  124. return current;
  125. }
  126. );
  127. var result = new CustomAggregator<TSource, TAccumulate>(source, function, source.Dispatcher);
  128. if (dependencyAnalysisMode == DependencyDiscovery.Enabled)
  129. {
  130. return result.DependsOnExpression(func, func.Parameters[1]);
  131. }
  132. return result;
  133. }
  134. }
  135. }