1
0

EnumerableEx.Imperative.cs 8.2 KB

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