11import Foundation
22
3- public extension Sequence {
4-
3+ public extension Sequence where Element : Sendable {
4+
55 /// Performs a map operation on the sequence's elements concurrently.
66 ///
77 /// The transformed elements are collected
@@ -15,14 +15,14 @@ public extension Sequence {
1515 /// - Returns: An array of transformed values `T`.
1616 /// - Throws: Rethrows any errors encountered during the filtering process. Due to implementation details,
1717 /// `parallelMap` can't use the `rethrows` keyword. In practice, it only throws if the `transform` function throws.
18- func parallelMap< T> (
18+ func parallelMap< T: Sendable > (
1919 preservingOrder: Bool = true ,
20- _ transform: @escaping ( Element ) async throws -> T
20+ _ transform: @Sendable @ escaping ( Element) async throws -> T
2121 ) async rethrows -> [ T ] {
2222 try await withThrowingTaskGroup ( of: ( Int, T) . self, returning: [ T ] . self) { group in
2323 var results : [ ( Int , T ) ] = [ ]
2424 results. reserveCapacity ( self . underestimatedCount)
25-
25+
2626 var index = 0
2727 for element in self {
2828 let indexCopy = index
@@ -31,11 +31,11 @@ public extension Sequence {
3131 }
3232 index += 1
3333 }
34-
34+
3535 for try await (index, element) in group {
3636 results. append ( ( index, element) )
3737 }
38-
38+
3939 return if preservingOrder {
4040 results
4141 . sorted ( by: \. 0 , ascending: true )
@@ -45,7 +45,7 @@ public extension Sequence {
4545 }
4646 }
4747 }
48-
48+
4949 /// Performs a filter operation on the sequence's elements concurrently.
5050 ///
5151 /// Elements that satisfy the closure (return `true`)
@@ -61,11 +61,11 @@ public extension Sequence {
6161 /// `parallelFilter` can't use the `rethrows` keyword. In practice, it only throws if the `isIncluded` function throws.
6262 func parallelFilter(
6363 preservingOrder: Bool = true ,
64- _ isIncluded: @escaping ( Element ) async throws -> Bool
64+ _ isIncluded: @Sendable @ escaping ( Element) async throws -> Bool
6565 ) async throws -> [ Element ] {
6666 try await withThrowingTaskGroup ( of: Optional< ( Int, Element) > . self , returning: [ Element ] . self) { group in
6767 var results = [ ( Int, Element) ] ( )
68-
68+
6969 var index = 0
7070 for element in self {
7171 let indexCopy = index
@@ -74,13 +74,13 @@ public extension Sequence {
7474 }
7575 index += 1
7676 }
77-
77+
7878 for try await result in group {
7979 if let ( index, element) = result {
8080 results. append ( ( index, element) )
8181 }
8282 }
83-
83+
8484 return if preservingOrder {
8585 results
8686 . sorted ( by: \. 0 , ascending: true )
0 commit comments