Skip to content

Commit 9f4b768

Browse files
committed
Add AsyncThrowingInterspersedSequence
1 parent 8738abd commit 9f4b768

File tree

3 files changed

+316
-32
lines changed

3 files changed

+316
-32
lines changed

Evolution/0011-interspersed.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,56 @@ public extension AsyncSequence {
9595
func interspersed(every: Int = 1, with separator: @Sendable @escaping () async -> Element) -> AsyncInterspersedSequence<Self> {
9696
AsyncInterspersedSequence(self, every: every, separator: separator)
9797
}
98+
99+
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
100+
/// the given separator between each element.
101+
///
102+
/// Any value of this asynchronous sequence's element type can be used as the separator.
103+
///
104+
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
105+
///
106+
/// ```
107+
/// let input = ["A", "B", "C"].async
108+
/// let interspersed = input.interspersed(with: "-")
109+
/// for await element in interspersed {
110+
/// print(element)
111+
/// }
112+
/// // Prints "A" "-" "B" "-" "C"
113+
/// ```
114+
///
115+
/// - Parameters:
116+
/// - every: Dictates after how many elements a separator should be inserted.
117+
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
118+
/// - Returns: The interspersed asynchronous sequence of elements.
119+
@inlinable
120+
public func interspersed(every: Int = 1, with separator: @Sendable @escaping () throws -> Element) -> AsyncThrowingInterspersedSequence<Self> {
121+
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
122+
}
123+
124+
/// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting
125+
/// the given separator between each element.
126+
///
127+
/// Any value of this asynchronous sequence's element type can be used as the separator.
128+
///
129+
/// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator:
130+
///
131+
/// ```
132+
/// let input = ["A", "B", "C"].async
133+
/// let interspersed = input.interspersed(with: "-")
134+
/// for await element in interspersed {
135+
/// print(element)
136+
/// }
137+
/// // Prints "A" "-" "B" "-" "C"
138+
/// ```
139+
///
140+
/// - Parameters:
141+
/// - every: Dictates after how many elements a separator should be inserted.
142+
/// - separator: A closure that produces the value to insert in between each of this async sequence’s elements.
143+
/// - Returns: The interspersed asynchronous sequence of elements.
144+
@inlinable
145+
public func interspersed(every: Int = 1, with separator: @Sendable @escaping () async throws -> Element) -> AsyncThrowingInterspersedSequence<Self> {
146+
AsyncThrowingInterspersedSequence(self, every: every, separator: separator)
147+
}
98148
}
99149
```
100150

0 commit comments

Comments
 (0)