1
0

EnumerableEx.Creation.cs 8.1 KB

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