EnumerableEx.Creation.cs 8.5 KB

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