|
2 | 2 | using System.Collections; |
3 | 3 | using System.Collections.Generic; |
4 | 4 | using System.Runtime.CompilerServices; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
5 | 7 |
|
6 | 8 | namespace PlainBytes.System.Extensions.Collections |
7 | 9 | { |
@@ -120,5 +122,71 @@ public static IEnumerable<T> Append<T>(this IEnumerable<T> collection, IEnumerab |
120 | 122 | yield return item; |
121 | 123 | } |
122 | 124 | } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Asynchronously enumerates the elements of the source sequence and invokes the specified asynchronous action |
| 128 | + /// for each element. |
| 129 | + /// </summary> |
| 130 | + /// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 131 | + /// <param name="source">The asynchronous sequence whose elements are to be processed.</param> |
| 132 | + /// <param name="action">An asynchronous delegate to invoke for each element in the source sequence.</param> |
| 133 | + /// <param name="token">A cancellation token that can be used to cancel the operation.</param> |
| 134 | + /// <returns>A task that represents the asynchronous operation. The task completes when all elements have been processed |
| 135 | + /// or the operation is canceled.</returns> |
| 136 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 137 | + public static async Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Func<T, CancellationToken, ValueTask> action, CancellationToken token = default) |
| 138 | + { |
| 139 | + await foreach (var item in source.WithCancellation(token)) |
| 140 | + { |
| 141 | + token.ThrowIfCancellationRequested(); |
| 142 | + |
| 143 | + await action(item, token).ConfigureAwait(false); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Iterates through the asynchronous sequence while processing the elements. |
| 150 | + /// </summary> |
| 151 | + /// <param name="source">The source asynchronous sequence to process.</param> |
| 152 | + /// <param name="selector">Transformer that processes the iterated items.</param> |
| 153 | + /// <param name="token">A cancellation token that can be used to cancel the asynchronous iteration.</param> |
| 154 | + /// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 155 | + /// <typeparam name="TR">Type of result item.</typeparam> |
| 156 | + /// <returns></returns> |
| 157 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 158 | + public static async IAsyncEnumerable<TR> SelectAsync<T, TR>(this IAsyncEnumerable<T> source, Func<T, TR> selector, [EnumeratorCancellation] CancellationToken token = default) |
| 159 | + { |
| 160 | + await foreach (var item in source.WithCancellation(token)) |
| 161 | + { |
| 162 | + token.ThrowIfCancellationRequested(); |
| 163 | + |
| 164 | + yield return selector(item); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /// <summary> |
| 169 | + /// Filters the elements of an asynchronous sequence based on a specified predicate. |
| 170 | + /// </summary> |
| 171 | + /// <typeparam name="T">The type of the elements in the source sequence.</typeparam> |
| 172 | + /// <param name="source">The source asynchronous sequence to filter.</param> |
| 173 | + /// <param name="predicate">A function to test each element for a condition. The element is included in the result if the function |
| 174 | + /// returns <see langword="true"/>.</param> |
| 175 | + /// <param name="token">A cancellation token that can be used to cancel the asynchronous iteration.</param> |
| 176 | + /// <returns>An asynchronous sequence that contains elements from the source sequence that satisfy the condition |
| 177 | + /// specified by the predicate.</returns> |
| 178 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 179 | + public static async IAsyncEnumerable<T> WhereAsync<T>(this IAsyncEnumerable<T> source, Func<T, bool> predicate, [EnumeratorCancellation] CancellationToken token = default) |
| 180 | + { |
| 181 | + await foreach (var item in source.WithCancellation(token)) |
| 182 | + { |
| 183 | + token.ThrowIfCancellationRequested(); |
| 184 | + |
| 185 | + if (predicate(item)) |
| 186 | + { |
| 187 | + yield return item; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
123 | 191 | } |
124 | 192 | } |
0 commit comments