You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Guides/Joined.md
+73-7Lines changed: 73 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -9,20 +9,72 @@
9
9
10
10
## Introduction
11
11
12
+
Concatenates an asynchronous sequence of asynchronous sequences that share an `Element` type together sequentially where the elements from the resulting asynchronous sequence are comprised in order from the elements of the first asynchronous sequence and then the second (and so on) or until an error occurs. Similar to `chain()`, except the number of asynchronous sequences to concatenate is not known up front.
13
+
14
+
Optionally allows inserting the elements of a separator asynchronous sequence in between each of the other sequences.
15
+
16
+
```swift
17
+
let sequenceOfURLs: AsyncSequence<URL> =...
18
+
let sequenceOfLines = sequenceOfURLs.map { $0.lines }
19
+
let joinedWithSeparator = sequenceOfLines.joined(separator: ["===================="].async)
20
+
21
+
fortryawait lineOrSeparator in joinedWithSeparator {
22
+
print(lineOrSeparator)
23
+
}
24
+
```
25
+
26
+
This example shows how an `AsyncSequence` of `URL`s can be turned into an `AsyncSequence` of the lines of each of those files in sequence, with a separator line in between each file.
The resulting `AsyncJoinedSequence` or `AsyncJoinedBySeparatorSequence` type is an asynchronous sequence, with conditional conformance to `Sendable` when the arguments conform.
110
+
111
+
When any of the asynchronous sequences being joined together come to their end of iteration, the `Joined` sequence iteration proceeds to the separator asynchronous sequence, if any. When the separator asynchronous sequence terminates, or if no sepaerator was specified, it proceeds on to the next asynchronous sequence. When the last asynchronous sequence reaches the end of iteration the `AsyncJoinedSequence` or `AsyncJoinedBySeparatorSequence` then ends its iteration. At any point in time if one of the comprising asynchronous sequences ever throws an error during iteration the `AsyncJoinedSequence` or `AsyncJoinedBySeparatorSequence` iteration will throw that error and end iteration.
112
+
113
+
## Future Directions
114
+
115
+
The Swift Algorithms package has [additional synchronous variants of `joined()`](https://github.com/apple/swift-algorithms/blob/main/Guides/Joined.md). It is conceivable to bring asynchronous variants of those over to `AsyncSequence`.
116
+
117
+
The variant that takes a single element as a separator is straightforward, but can be trivially replicated with `[element].async`. However, it may be beneficial for performance to reimplement this directly.
118
+
119
+
There is another variant that takes a closure that allows one to customize the separator based on the return value of a closure. That closure is passed each of the two consecutive asynchronous sequences (not the two neighboring elements in consecutive sequences). This variant arguably has the greatest utility when the sequence type conforms to `Collection`, allowing either the `count` or any arbitrary element to be obtained directly. With `AsyncSequence`, it is less likely that this function with provide a similar level of utility, so it has been omitted.
120
+
57
121
## Alternatives Considered
58
122
123
+
Because `joined()` is essentially identical to `flatMap { $0 }`, it was considered that this should be called `flatten()` instead. However, it is preferable to follow the lead of the Swift standard library's `joined()` method.
124
+
59
125
## Credits/Inspiration
60
126
61
-
The Swift standard library has a [function on synchronous sequences](https://developer.apple.com/documentation/swift/sequence/1641166-joined)that performs a similar (but synchronous) task.
127
+
The Swift standard library has functions on synchronous sequences ([`joined()`](https://developer.apple.com/documentation/swift/sequence/1641166-joined), [`joined(separator:)`](https://developer.apple.com/documentation/swift/sequence/2431985-joined)) that perform similar (but synchronous) tasks.
0 commit comments