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