EnumerableEx.Imperative.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. namespace System.Linq
  9. {
  10. public static partial class EnumerableEx
  11. {
  12. /// <summary>
  13. /// Generates an enumerable sequence by repeating a source sequence as long as the given loop condition holds.
  14. /// </summary>
  15. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  16. /// <param name="condition">Loop condition.</param>
  17. /// <param name="source">Sequence to repeat while the condition evaluates true.</param>
  18. /// <returns>Sequence generated by repeating the given sequence while the condition evaluates to true.</returns>
  19. public static IEnumerable<TResult> While<TResult>(Func<bool> condition, IEnumerable<TResult> source)
  20. {
  21. if (condition == null)
  22. throw new ArgumentNullException(nameof(condition));
  23. if (source == null)
  24. throw new ArgumentNullException(nameof(source));
  25. return WhileCore(condition, source).Concat();
  26. }
  27. static IEnumerable<IEnumerable<TSource>> WhileCore<TSource>(Func<bool> condition, IEnumerable<TSource> source)
  28. {
  29. while (condition())
  30. yield return source;
  31. }
  32. /// <summary>
  33. /// Returns an enumerable sequence based on the evaluation result of the given condition.
  34. /// </summary>
  35. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  36. /// <param name="condition">Condition to evaluate.</param>
  37. /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
  38. /// <param name="elseSource">Sequence to return in case the condition evaluates false.</param>
  39. /// <returns>Either of the two input sequences based on the result of evaluating the condition.</returns>
  40. public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource, IEnumerable<TResult> elseSource)
  41. {
  42. if (condition == null)
  43. throw new ArgumentNullException(nameof(condition));
  44. if (thenSource == null)
  45. throw new ArgumentNullException(nameof(thenSource));
  46. if (elseSource == null)
  47. throw new ArgumentNullException(nameof(elseSource));
  48. return EnumerableEx.Defer(() => condition() ? thenSource : elseSource);
  49. }
  50. /// <summary>
  51. /// Returns an enumerable sequence if the evaluation result of the given condition is true, otherwise returns an empty sequence.
  52. /// </summary>
  53. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  54. /// <param name="condition">Condition to evaluate.</param>
  55. /// <param name="thenSource">Sequence to return in case the condition evaluates true.</param>
  56. /// <returns>The given input sequence if the condition evaluates true; otherwise, an empty sequence.</returns>
  57. public static IEnumerable<TResult> If<TResult>(Func<bool> condition, IEnumerable<TResult> thenSource)
  58. {
  59. if (condition == null)
  60. throw new ArgumentNullException(nameof(condition));
  61. if (thenSource == null)
  62. throw new ArgumentNullException(nameof(thenSource));
  63. return EnumerableEx.Defer(() => condition() ? thenSource : Enumerable.Empty<TResult>());
  64. }
  65. /// <summary>
  66. /// Generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds.
  67. /// </summary>
  68. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  69. /// <param name="source">Source sequence to repeat while the condition evaluates true.</param>
  70. /// <param name="condition">Loop condition.</param>
  71. /// <returns>Sequence generated by repeating the given sequence until the condition evaluates to false.</returns>
  72. public static IEnumerable<TResult> DoWhile<TResult>(this IEnumerable<TResult> source, Func<bool> condition)
  73. {
  74. if (source == null)
  75. throw new ArgumentNullException(nameof(source));
  76. if (condition == null)
  77. throw new ArgumentNullException(nameof(condition));
  78. return source.Concat(While(condition, source));
  79. }
  80. /// <summary>
  81. /// Returns a sequence from a dictionary based on the result of evaluating a selector function.
  82. /// </summary>
  83. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  84. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  85. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  86. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  87. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, an empty sequence.</returns>
  88. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources)
  89. {
  90. if (selector == null)
  91. throw new ArgumentNullException(nameof(selector));
  92. if (sources == null)
  93. throw new ArgumentNullException(nameof(sources));
  94. return Case(selector, sources, Enumerable.Empty<TResult>());
  95. }
  96. /// <summary>
  97. /// Returns a sequence from a dictionary based on the result of evaluating a selector function, also specifying a default sequence.
  98. /// </summary>
  99. /// <typeparam name="TValue">Type of the selector value.</typeparam>
  100. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  101. /// <param name="selector">Selector function used to pick a sequence from the given sources.</param>
  102. /// <param name="sources">Dictionary mapping selector values onto resulting sequences.</param>
  103. /// <param name="defaultSource">Default sequence to return in case there's no corresponding source for the computed selector value.</param>
  104. /// <returns>The source sequence corresponding with the evaluated selector value; otherwise, the default source.</returns>
  105. public static IEnumerable<TResult> Case<TValue, TResult>(Func<TValue> selector, IDictionary<TValue, IEnumerable<TResult>> sources, IEnumerable<TResult> defaultSource)
  106. {
  107. if (selector == null)
  108. throw new ArgumentNullException(nameof(selector));
  109. if (sources == null)
  110. throw new ArgumentNullException(nameof(sources));
  111. if (defaultSource == null)
  112. throw new ArgumentNullException(nameof(defaultSource));
  113. return EnumerableEx.Defer(() =>
  114. {
  115. IEnumerable<TResult> result;
  116. if (!sources.TryGetValue(selector(), out result))
  117. result = defaultSource;
  118. return result;
  119. });
  120. }
  121. /// <summary>
  122. /// Generates a sequence by enumerating a source sequence, mapping its elements on result sequences, and concatenating those sequences.
  123. /// </summary>
  124. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  125. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  126. /// <param name="source">Source sequence.</param>
  127. /// <param name="resultSelector">Result selector to evaluate for each iteration over the source.</param>
  128. /// <returns>Sequence concatenating the inner sequences that result from evaluating the result selector on elements from the source.</returns>
  129. public static IEnumerable<TResult> For<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  130. {
  131. if (source == null)
  132. throw new ArgumentNullException(nameof(source));
  133. if (resultSelector == null)
  134. throw new ArgumentNullException(nameof(resultSelector));
  135. return ForCore(source, resultSelector).Concat();
  136. }
  137. static IEnumerable<IEnumerable<TResult>> ForCore<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> resultSelector)
  138. {
  139. return source.Select(resultSelector);
  140. }
  141. }
  142. }