EnumerableEx.Creation.cs 7.8 KB

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