EnumerableEx.Creation.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. namespace System.Linq
  7. {
  8. public static partial class EnumerableEx
  9. {
  10. /// <summary>
  11. /// Creates an enumerable sequence based on an enumerator factory function.
  12. /// </summary>
  13. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  14. /// <param name="getEnumerator">Enumerator factory function.</param>
  15. /// <returns>Sequence that will invoke the enumerator factory upon a call to GetEnumerator.</returns>
  16. public static IEnumerable<TResult> Create<TResult>(Func<IEnumerator<TResult>> getEnumerator)
  17. {
  18. if (getEnumerator == null)
  19. throw new ArgumentNullException("getEnumerator");
  20. return new AnonymousEnumerable<TResult>(getEnumerator);
  21. }
  22. #if HAS_AWAIT
  23. /// <summary>
  24. /// Creates an enumerable sequence based on an asynchronous method that provides a yielder.
  25. /// </summary>
  26. /// <typeparam name="T">Result sequence element type.</typeparam>
  27. /// <param name="create">Delegate implementing an asynchronous method that can use the specified yielder to yield return values.</param>
  28. /// <returns>Sequence that will use the asynchronous method to obtain its elements.</returns>
  29. public static IEnumerable<T> Create<T>(Action<IYielder<T>> create)
  30. {
  31. if (create == null)
  32. throw new ArgumentNullException("create");
  33. foreach (var x in new Yielder<T>(create))
  34. yield return x;
  35. }
  36. #endif
  37. class AnonymousEnumerable<TResult> : IEnumerable<TResult>
  38. {
  39. private readonly Func<IEnumerator<TResult>> _getEnumerator;
  40. public AnonymousEnumerable(Func<IEnumerator<TResult>> getEnumerator)
  41. {
  42. _getEnumerator = getEnumerator;
  43. }
  44. public IEnumerator<TResult> GetEnumerator()
  45. {
  46. return _getEnumerator();
  47. }
  48. Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
  49. {
  50. return GetEnumerator();
  51. }
  52. }
  53. /// <summary>
  54. /// Returns a sequence with a single element.
  55. /// </summary>
  56. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  57. /// <param name="value">Single element of the resulting sequence.</param>
  58. /// <returns>Sequence with a single element.</returns>
  59. public static IEnumerable<TResult> Return<TResult>(TResult value)
  60. {
  61. yield return value;
  62. }
  63. /// <summary>
  64. /// Returns a sequence that throws an exception upon enumeration.
  65. /// </summary>
  66. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  67. /// <param name="exception">Exception to throw upon enumerating the resulting sequence.</param>
  68. /// <returns>Sequence that throws the specified exception upon enumeration.</returns>
  69. public static IEnumerable<TResult> Throw<TResult>(Exception exception)
  70. {
  71. if (exception == null)
  72. throw new ArgumentNullException("exception");
  73. return Throw_<TResult>(exception);
  74. }
  75. private static IEnumerable<TResult> Throw_<TResult>(Exception exception)
  76. {
  77. throw exception;
  78. #pragma warning disable 0162
  79. yield break;
  80. #pragma warning restore 0162
  81. }
  82. /// <summary>
  83. /// Creates an enumerable sequence based on an enumerable factory function.
  84. /// </summary>
  85. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  86. /// <param name="enumerableFactory">Enumerable factory function.</param>
  87. /// <returns>Sequence that will invoke the enumerable factory upon a call to GetEnumerator.</returns>
  88. public static IEnumerable<TResult> Defer<TResult>(Func<IEnumerable<TResult>> enumerableFactory)
  89. {
  90. if (enumerableFactory == null)
  91. throw new ArgumentNullException("enumerableFactory");
  92. return Defer_(enumerableFactory);
  93. }
  94. private static IEnumerable<TSource> Defer_<TSource>(Func<IEnumerable<TSource>> enumerableFactory)
  95. {
  96. foreach (var item in enumerableFactory())
  97. yield return item;
  98. }
  99. /// <summary>
  100. /// Generates a sequence by mimicking a for loop.
  101. /// </summary>
  102. /// <typeparam name="TState">State type.</typeparam>
  103. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  104. /// <param name="initialState">Initial state of the generator loop.</param>
  105. /// <param name="condition">Loop condition.</param>
  106. /// <param name="iterate">State update function to run after every iteration of the generator loop.</param>
  107. /// <param name="resultSelector">Result selector to compute resulting sequence elements.</param>
  108. /// <returns>Sequence obtained by running the generator loop, yielding computed elements.</returns>
  109. public static IEnumerable<TResult> Generate<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  110. {
  111. if (condition == null)
  112. throw new ArgumentNullException("condition");
  113. if (iterate == null)
  114. throw new ArgumentNullException("iterate");
  115. if (resultSelector == null)
  116. throw new ArgumentNullException("resultSelector");
  117. return Generate_(initialState, condition, iterate, resultSelector);
  118. }
  119. private static IEnumerable<TResult> Generate_<TState, TResult>(TState initialState, Func<TState, bool> condition, Func<TState, TState> iterate, Func<TState, TResult> resultSelector)
  120. {
  121. for (var i = initialState; condition(i); i = iterate(i))
  122. yield return resultSelector(i);
  123. }
  124. /// <summary>
  125. /// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence usage duration.
  126. /// </summary>
  127. /// <typeparam name="TSource">Source element type.</typeparam>
  128. /// <typeparam name="TResource">Resource type.</typeparam>
  129. /// <param name="resourceFactory">Resource factory function.</param>
  130. /// <param name="enumerableFactory">Enumerable factory function, having access to the obtained resource.</param>
  131. /// <returns>Sequence whose use controls the lifetime of the associated obtained resource.</returns>
  132. public static IEnumerable<TSource> Using<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  133. {
  134. if (resourceFactory == null)
  135. throw new ArgumentNullException("resourceFactory");
  136. if (enumerableFactory == null)
  137. throw new ArgumentNullException("enumerableFactory");
  138. return Using_(resourceFactory, enumerableFactory);
  139. }
  140. private static IEnumerable<TSource> Using_<TSource, TResource>(Func<TResource> resourceFactory, Func<TResource, IEnumerable<TSource>> enumerableFactory) where TResource : IDisposable
  141. {
  142. using (var res = resourceFactory())
  143. foreach (var item in enumerableFactory(res))
  144. yield return item;
  145. }
  146. /// <summary>
  147. /// Generates a sequence by repeating the given value infinitely.
  148. /// </summary>
  149. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  150. /// <param name="value">Value to repreat in the resulting sequence.</param>
  151. /// <returns>Sequence repeating the given value infinitely.</returns>
  152. public static IEnumerable<TResult> Repeat<TResult>(TResult value)
  153. {
  154. while (true)
  155. yield return value;
  156. }
  157. /// <summary>
  158. /// Generates a sequence that contains one repeated value.
  159. /// </summary>
  160. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  161. /// <param name="element">The value to be repeated.</param>
  162. /// <param name="count">The number of times to repeat the value in the generated sequence.</param>
  163. /// <returns>Sequence that contains a repeated value.</returns>
  164. public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count)
  165. {
  166. return Enumerable.Repeat<TResult>(element, count);
  167. }
  168. }
  169. }