Skip to content

Commit 2e6a72b

Browse files
authored
Better documentation + improved module naming (#8)
1 parent 2860a42 commit 2e6a72b

File tree

5 files changed

+104
-55
lines changed

5 files changed

+104
-55
lines changed

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ let package = Package(
1313
],
1414
products: [
1515
.library(
16-
name: "SwiftConcurrentSequence",
17-
targets: ["SwiftConcurrentSequence"]
16+
name: "ConcurrentSequence",
17+
targets: ["ConcurrentSequence"]
1818
),
1919
],
2020
targets: [
2121
.target(
22-
name: "SwiftConcurrentSequence",
22+
name: "ConcurrentSequence",
2323
dependencies: [],
2424
swiftSettings: [
2525
.swiftLanguageMode(.v6),
2626
]
2727
),
2828
.testTarget(
29-
name: "SwiftConcurrentSequenceTests",
30-
dependencies: ["SwiftConcurrentSequence"],
29+
name: "ConcurrentSequenceTests",
30+
dependencies: ["ConcurrentSequence"],
3131
swiftSettings: [
3232
.swiftLanguageMode(.v6),
3333
]

Sources/SwiftConcurrentSequence/ConcurrentMap.swift renamed to Sources/ConcurrentSequence/ConcurrentMap.swift

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,38 @@ extension Sequence where Element: Sendable {
3030
/// over the sequence's elements. The given closure is executed concurrently
3131
/// on multiple queues to reduce the wall-time consumed by the transform.
3232
///
33+
/// Example:
34+
/// ```swift
35+
/// let numbers = [1, 2, 3, 4, 5]
36+
/// let squared = numbers.concurrentMap { $0 * $0 }
37+
/// // Result: [1, 4, 9, 16, 25]
38+
/// ```
39+
///
3340
/// - Parameter transform: A mapping closure. `transform` accepts an
3441
/// element of this sequence as its parameter and returns a transformed
3542
/// value of the same or of a different type.
36-
/// - Returns: An array containing the transformed elements of this
37-
/// sequence.
43+
/// - Returns: An array containing the transformed elements of this sequence in their original order.
3844
public func concurrentMap<T: Sendable>(_ transform: @Sendable (Element) -> T) -> [T] {
3945
Array(self).concurrentMap(transform)
4046
}
4147
#endif
4248

43-
/// Returns an array containing the results of mapping the given closure
49+
/// Returns an array containing the results of mapping the given async closure
4450
/// over the sequence's elements. The given closure is executed concurrently
45-
/// on multiple queues to reduce the wall-time consumed by the transform.
51+
/// using Swift's structured concurrency to reduce wall-time.
52+
///
53+
/// Example:
54+
/// ```swift
55+
/// let urls = ["url1", "url2", "url3"]
56+
/// let responses = await urls.concurrentMap { url in
57+
/// try await fetchData(from: url)
58+
/// }
59+
/// ```
4660
///
47-
/// - Parameter transform: A mapping closure. `transform` accepts an
61+
/// - Parameter transform: An async mapping closure. `transform` accepts an
4862
/// element of this sequence as its parameter and returns a transformed
4963
/// value of the same or of a different type.
50-
/// - Returns: An array containing the transformed elements of this
51-
/// sequence.
64+
/// - Returns: An array containing the transformed elements of this sequence in their original order.
5265
@_disfavoredOverload
5366
public func concurrentMap<T: Sendable>(_ transform: @escaping @Sendable (Element) async throws -> T) async rethrows -> [T] {
5467
try await Array(self).concurrentMap(transform)
@@ -63,11 +76,17 @@ extension Array where Element: Sendable {
6376
/// over the sequence's elements. The given closure is executed concurrently
6477
/// on multiple queues to reduce the wall-time consumed by the transform.
6578
///
79+
/// Example:
80+
/// ```swift
81+
/// let numbers = [1, 2, 3, 4, 5]
82+
/// let squared = numbers.concurrentMap { $0 * $0 }
83+
/// // Result: [1, 4, 9, 16, 25]
84+
/// ```
85+
///
6686
/// - Parameter transform: A mapping closure. `transform` accepts an
6787
/// element of this sequence as its parameter and returns a transformed
6888
/// value of the same or of a different type.
69-
/// - Returns: An array containing the transformed elements of this
70-
/// sequence.
89+
/// - Returns: An array containing the transformed elements of this sequence in their original order.
7190
public func concurrentMap<T: Sendable>(_ transform: @Sendable (Element) -> T) -> [T] {
7291
// Create a buffer where we can store the transformed output.
7392
var transformed = [T?](repeating: nil, count: count)
@@ -83,15 +102,22 @@ extension Array where Element: Sendable {
83102
}
84103
#endif
85104

86-
/// Returns an array containing the results of mapping the given closure
105+
/// Returns an array containing the results of mapping the given async closure
87106
/// over the sequence's elements. The given closure is executed concurrently
88-
/// on multiple queues to reduce the wall-time consumed by the transform.
107+
/// using Swift's structured concurrency to reduce wall-time.
108+
///
109+
/// Example:
110+
/// ```swift
111+
/// let endpoints: [URL] = […]
112+
/// let responses = await endpoints.concurrentMap { url in
113+
/// try await fetchData(from: url)
114+
/// }
115+
/// ```
89116
///
90-
/// - Parameter transform: A mapping closure. `transform` accepts an
117+
/// - Parameter transform: An async mapping closure. `transform` accepts an
91118
/// element of this sequence as its parameter and returns a transformed
92119
/// value of the same or of a different type.
93-
/// - Returns: An array containing the transformed elements of this
94-
/// sequence.
120+
/// - Returns: An array containing the transformed elements of this sequence in their original order.
95121
@_disfavoredOverload
96122
public func concurrentMap<T: Sendable>(_ transform: @escaping @Sendable (Element) async throws -> T) async rethrows -> [T] {
97123
try await withThrowingTaskGroup(

Sources/SwiftConcurrentSequence/ConcurrentReduce.swift renamed to Sources/ConcurrentSequence/ConcurrentReduce.swift

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ extension Sequence where Element: Sendable {
3030
/// given closure. The given closure is executed concurrently
3131
/// on multiple queues to reduce the wall-time consumed by the reduction.
3232
///
33-
/// Use the `reduce(into:_:)` method to produce a single value from the
34-
/// elements of an entire sequence. For example, you can use this method on an
35-
/// array of integers to filter adjacent equal entries or count frequencies.
33+
/// This synchronous method uses Grand Central Dispatch to parallelize the reduction
34+
/// by recursively combining pairs of elements until a single result remains.
35+
///
36+
/// Example:
37+
/// ```swift
38+
/// let numbers = [1, 2, 3, 4, 5]
39+
/// let sum = numbers.concurrentReduce(defaultValue: 0) { accumulator, value in
40+
/// accumulator += value
41+
/// }
42+
/// // Result: 15
43+
/// ```
3644
///
3745
/// - Parameters:
3846
/// - defaultValue: A default value for Element. This value is utilized only
@@ -74,17 +82,24 @@ extension Sequence where Element: Sendable {
7482
/// using the given closure. The given closure is executed concurrently
7583
/// on multiple queues to reduce the wall-time consumed by the reduction.
7684
///
77-
/// Use the `reduce(into:_:)` method to produce a single value from the
78-
/// elements of an entire sequence. For example, you can use this method on an
79-
/// array of integers to filter adjacent equal entries or count frequencies.
85+
/// This specialized method merges dictionaries with custom conflict resolution
86+
/// for duplicate keys.
8087
///
81-
/// - Parameters:
82-
/// - defaultValue: A default value for Element. This value is utilized only
83-
/// when the receiver is empty.
84-
/// - combine: A closure that returns the desired value for
85-
/// the given key when multiple values are present for the given key.
86-
/// - Returns: The final reduced value. If the sequence has no elements,
87-
/// the result is `defaultValue`.
88+
/// Example:
89+
/// ```swift
90+
/// let dictionaries = [
91+
/// ["apple": 1, "banana": 2],
92+
/// ["apple": 3, "cherry": 5]
93+
/// ]
94+
/// let merged = dictionaries.concurrentReduce { key, first, second in
95+
/// return first + second // Sum values for duplicate keys
96+
/// }
97+
/// // Result: ["apple": 4, "banana": 2, "cherry": 5]
98+
/// ```
99+
///
100+
/// - Parameter combine: A closure that returns the desired value for
101+
/// the given key when multiple values are present for the given key.
102+
/// - Returns: The final reduced dictionary.
88103
public func concurrentReduce<Key, Value>(
89104
combine: @Sendable (Key, Value, Value) -> Value
90105
) -> Element where Element == [Key: Value] {
@@ -101,20 +116,24 @@ extension Sequence where Element: Sendable {
101116
#endif
102117

103118
/// Returns the result of combining the elements of the sequence using the
104-
/// given closure. The given closure is executed concurrently
105-
/// on multiple queues to reduce the wall-time consumed by the reduction.
119+
/// given async closure. The given closure is executed concurrently
120+
/// using Swift's structured concurrency to reduce wall-time.
106121
///
107-
/// Use the `reduce(into:_:)` method to produce a single value from the
108-
/// elements of an entire sequence. For example, you can use this method on an
109-
/// array of integers to filter adjacent equal entries or count frequencies.
122+
/// This asynchronous method uses task groups to parallelize the reduction
123+
/// by recursively combining pairs of elements until a single result remains.
124+
///
125+
/// Example:
126+
/// ```swift
127+
/// let files: [URL] = […]
128+
/// let mergedFileContents = await endpoints.concurrentReduce(defaultValue: FileContents()) { accumulated, file in
129+
/// try await accumulated.merge(readData(from: file))
130+
/// }
131+
/// ```
110132
///
111133
/// - Parameters:
112134
/// - defaultValue: A default value for Element. This value is utilized only
113135
/// when the receiver is empty.
114-
/// - reducingIntoFirst: A closure that combines an accumulating value and
115-
/// an element of the sequence into a new reduced value, to be used
116-
/// in the next call of the `reducingIntoFirst` closure or returned to
117-
/// the caller.
136+
/// - reducer: A closure that combines two elements into a new reduced value.
118137
/// - Returns: The final reduced value. If the sequence has no elements,
119138
/// the result is `defaultValue`.
120139
public func concurrentReduce(
@@ -155,20 +174,24 @@ extension Sequence where Element: Sendable {
155174
}
156175

157176
/// Returns the result of combining the elements of the sequence of dictionaries
158-
/// using the given closure. The given closure is executed concurrently
159-
/// on multiple queues to reduce the wall-time consumed by the reduction.
177+
/// using the given async closure. The given closure is executed concurrently
178+
/// using Swift's structured concurrency to reduce wall-time.
160179
///
161-
/// Use the `reduce(into:_:)` method to produce a single value from the
162-
/// elements of an entire sequence. For example, you can use this method on an
163-
/// array of integers to filter adjacent equal entries or count frequencies.
180+
/// This specialized async method merges dictionaries with custom conflict resolution
181+
/// for duplicate keys.
164182
///
165-
/// - Parameters:
166-
/// - defaultValue: A default value for Element. This value is utilized only
167-
/// when the receiver is empty.
168-
/// - combine: A closure that returns the desired value for
169-
/// the given key when multiple values are present for the given key.
170-
/// - Returns: The final reduced value. If the sequence has no elements,
171-
/// the result is `defaultValue`.
183+
/// Example:
184+
/// ```swift
185+
/// let userGroups = await fetchUserGroups() // Returns [[String: User]]
186+
/// let mergedUsers = await userGroups.concurrentReduce { key, user1, user2 in
187+
/// // Custom logic to merge duplicate users
188+
/// return user1.updatedAt > user2.updatedAt ? user1 : user2
189+
/// }
190+
/// ```
191+
///
192+
/// - Parameter combine: A closure that returns the desired value for
193+
/// the given key when multiple values are present for the given key.
194+
/// - Returns: The final reduced dictionary.
172195
public func concurrentReduce<Key, Value>(
173196
combine: @escaping @Sendable (Key, Value, Value) throws -> Value
174197
) async rethrows -> Element where Element == [Key: Value] {

Tests/SwiftConcurrentSequenceTests/ConcurrentMapTests.swift renamed to Tests/ConcurrentSequenceTests/ConcurrentMapTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import SwiftConcurrentSequence
23+
import ConcurrentSequence
2424
import Testing
2525

2626
struct ConcurrentMapTests {

Tests/SwiftConcurrentSequenceTests/ConcurrentReduceTests.swift renamed to Tests/ConcurrentSequenceTests/ConcurrentReduceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import SwiftConcurrentSequence
23+
import ConcurrentSequence
2424
import Testing
2525

2626
struct ConcurrentReduceTests {

0 commit comments

Comments
 (0)