2222// Sequence+concurrency.swift
2323// DynamoDBTables
2424//
25- //
26- //
27- //
2825
2926// MARK: - ForEach
3027
@@ -45,87 +42,11 @@ extension Sequence {
4542 try await operation ( element)
4643 }
4744 }
48- /*
49- /// Run an async closure for each element within the sequence.
50- ///
51- /// The closure calls will be performed concurrently, but the call
52- /// to this function won't return until all of the closure calls
53- /// have completed.
54- ///
55- /// - parameter priority: Any specific `TaskPriority` to assign to
56- /// the async tasks that will perform the closure calls. The
57- /// default is `nil` (meaning that the system picks a priority).
58- /// - parameter operation: The closure to run for each element.
59- func concurrentForEach(
60- withPriority priority: TaskPriority? = nil,
61- _ operation: @Sendable @escaping (Element) async -> Void
62- ) async {
63- await withTaskGroup(of: Void.self) { group in
64- for element in self {
65- group.addTask(priority: priority) {
66- await operation(element)
67- }
68- }
69- }
70- }
71-
72- /// Run an async closure for each element within the sequence.
73- ///
74- /// The closure calls will be performed concurrently, but the call
75- /// to this function won't return until all of the closure calls
76- /// have completed. If any of the closure calls throw an error,
77- /// then the first error will be rethrown once all closure calls have
78- /// completed.
79- ///
80- /// - parameter priority: Any specific `TaskPriority` to assign to
81- /// the async tasks that will perform the closure calls. The
82- /// default is `nil` (meaning that the system picks a priority).
83- /// - parameter operation: The closure to run for each element.
84- /// - throws: Rethrows any error thrown by the passed closure.
85- func concurrentForEach(
86- withPriority priority: TaskPriority? = nil,
87- _ operation: @Sendable @escaping (Element) async throws -> Void
88- ) async throws {
89- try await withThrowingTaskGroup(of: Void.self) { group in
90- for element in self {
91- group.addTask(priority: priority) {
92- try await operation(element)
93- }
94- }
95-
96- // Propagate any errors thrown by the group's tasks:
97- for try await _ in group {}
98- }
99- }*/
10045}
10146
10247// MARK: - Map
10348
10449extension Sequence where Element: Sendable {
105- /* /// Transform the sequence into an array of new values using
106- /// an async closure.
107- ///
108- /// The closure calls will be performed in order, by waiting for
109- /// each call to complete before proceeding with the next one. If
110- /// any of the closure calls throw an error, then the iteration
111- /// will be terminated and the error rethrown.
112- ///
113- /// - parameter transform: The transform to run on each element.
114- /// - returns: The transformed values as an array. The order of
115- /// the transformed values will match the original sequence.
116- /// - throws: Rethrows any error thrown by the passed closure.
117- func asyncMap<T>(
118- _ transform: @Sendable (Element) async throws -> T
119- ) async rethrows -> [T] {
120- var values = [T]()
121-
122- for element in self {
123- try await values.append(transform(element))
124- }
125-
126- return values
127- }
128- */
12950 /// Transform the sequence into an array of new values using
13051 /// an async closure.
13152 ///
@@ -157,7 +78,7 @@ extension Sequence where Element: Sendable {
15778 while let next = await group. next ( ) {
15879 res [ next. offset] = next. value
15980 }
160- return res as! [ T ]
81+ return res. asNonOptionalCollection ( )
16182 }
16283 }
16384
@@ -195,7 +116,7 @@ extension Sequence where Element: Sendable {
195116 while let next = try await group. next ( ) {
196117 res [ next. offset] = next. value
197118 }
198- return res as! [ T ]
119+ return res. asNonOptionalCollection ( )
199120 }
200121 }
201122}
@@ -266,7 +187,7 @@ extension Sequence where Element: Sendable {
266187 while let next = await group. next ( ) {
267188 res [ next. offset] = next. value
268189 }
269- return ( res as! [ T ? ] ) . compactMap ( \. self)
190+ return ( res. asNonOptionalCollection ( ) ) . compactMap ( \. self)
270191 }
271192 }
272193
@@ -306,41 +227,14 @@ extension Sequence where Element: Sendable {
306227 while let next = try await group. next ( ) {
307228 res [ next. offset] = next. value
308229 }
309- return ( res as! [ T ? ] ) . compactMap ( \. self)
230+ return ( res. asNonOptionalCollection ( ) ) . compactMap ( \. self)
310231 }
311232 }
312233}
313234
314235// MARK: - FlatMap
315236
316237extension Sequence where Element: Sendable {
317- /* /// Transform the sequence into an array of new values using
318- /// an async closure that returns sequences. The returned sequences
319- /// will be flattened into the array returned from this function.
320- ///
321- /// The closure calls will be performed in order, by waiting for
322- /// each call to complete before proceeding with the next one. If
323- /// any of the closure calls throw an error, then the iteration
324- /// will be terminated and the error rethrown.
325- ///
326- /// - parameter transform: The transform to run on each element.
327- /// - returns: The transformed values as an array. The order of
328- /// the transformed values will match the original sequence,
329- /// with the results of each closure call appearing in-order
330- /// within the returned array.
331- /// - throws: Rethrows any error thrown by the passed closure.
332- func asyncFlatMap<T: Sequence>(
333- _ transform: @Sendable (Element) async throws -> T
334- ) async rethrows -> [T.Element] {
335- var values = [T.Element]()
336-
337- for element in self {
338- try await values.append(contentsOf: transform(element))
339- }
340-
341- return values
342- }
343- */
344238 /// Transform the sequence into an array of new values using
345239 /// an async closure that returns sequences. The returned sequences
346240 /// will be flattened into the array returned from this function.
@@ -375,7 +269,7 @@ extension Sequence where Element: Sendable {
375269 while let next = await group. next ( ) {
376270 res [ next. offset] = next. value
377271 }
378- return ( res as! [ T ] ) . flatMap ( \. self)
272+ return ( res. asNonOptionalCollection ( ) ) . flatMap ( \. self)
379273 }
380274 }
381275
@@ -416,7 +310,19 @@ extension Sequence where Element: Sendable {
416310 while let next = try await group. next ( ) {
417311 res [ next. offset] = next. value
418312 }
419- return ( res as! [ T ] ) . flatMap ( \. self)
313+ return ( res. asNonOptionalCollection ( ) ) . flatMap ( \. self)
314+ }
315+ }
316+ }
317+
318+ extension Collection {
319+ func asNonOptionalCollection< T> ( ) -> [ T ] where Element == Optional < T > {
320+ return self . map { optional in
321+ guard let finalValue = optional else {
322+ fatalError ( " Mapped task did not complete as expected " )
323+ }
324+
325+ return finalValue
420326 }
421327 }
422328}
0 commit comments