|
| 1 | +# Feature name |
| 2 | + |
| 3 | +* Proposal: [SAA-0011](https://github.com/apple/swift-async-algorithms/blob/main/Evolution/0011-interspersed.md) |
| 4 | +* Authors: [Philippe Hausler](https://github.com/phausler) |
| 5 | +* Review Manager: [Franz Busch](https://github.com/FranzBusch) |
| 6 | +* Status: **Implemented** |
| 7 | + |
| 8 | +* Implementation: |
| 9 | + [Source](https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Interspersed/AsyncInterspersedSequence.swift) | |
| 10 | + [Tests](https://github.com/apple/swift-async-algorithms/blob/main/Tests/AsyncAlgorithmsTests/TestLazy.swift) |
| 11 | + |
| 12 | +## Motivation |
| 13 | + |
| 14 | +A common transformation that is applied to async sequences is to intersperse the elements with |
| 15 | +a separator element. |
| 16 | + |
| 17 | +## Proposed solution |
| 18 | + |
| 19 | +We propose to add a new method on `AsyncSequence` that allows to intersperse |
| 20 | +a separator between each emitted element. This proposed API looks like this |
| 21 | + |
| 22 | +```swift |
| 23 | +extension AsyncSequence { |
| 24 | + /// Returns a new asynchronous sequence containing the elements of this asynchronous sequence, inserting |
| 25 | + /// the given separator between each element. |
| 26 | + /// |
| 27 | + /// Any value of this asynchronous sequence's element type can be used as the separator. |
| 28 | + /// |
| 29 | + /// The following example shows how an async sequences of `String`s can be interspersed using `-` as the separator: |
| 30 | + /// |
| 31 | + /// ``` |
| 32 | + /// let input = ["A", "B", "C"].async |
| 33 | + /// let interspersed = input.interspersed(with: "-") |
| 34 | + /// for await element in interspersed { |
| 35 | + /// print(element) |
| 36 | + /// } |
| 37 | + /// // Prints "A" "-" "B" "-" "C" |
| 38 | + /// ``` |
| 39 | + /// |
| 40 | + /// - Parameter separator: The value to insert in between each of this async |
| 41 | + /// sequence’s elements. |
| 42 | + /// - Returns: The interspersed asynchronous sequence of elements. |
| 43 | + @inlinable |
| 44 | + public func interspersed(with separator: Element) -> AsyncInterspersedSequence<Self> { |
| 45 | + AsyncInterspersedSequence(self, separator: separator) |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +## Detailed design |
| 51 | + |
| 52 | +The bulk of the implementation of the new `interspersed` method is inside the new |
| 53 | +`AsyncInterspersedSequence` struct. It constructs an iterator to the base async sequence |
| 54 | +inside its own iterator. The `AsyncInterspersedSequence.Iterator.next()` is forwarding the demand |
| 55 | +to the base iterator. |
| 56 | +There is one special case that we have to call out. When the base async sequence throws |
| 57 | +then `AsyncInterspersedSequence.Iterator.next()` will return the separator first and then rethrow the error. |
| 58 | + |
| 59 | +Below is the implementation of the `AsyncInterspersedSequence`. |
| 60 | +```swift |
| 61 | +/// An asynchronous sequence that presents the elements of a base asynchronous sequence of |
| 62 | +/// elements with a separator between each of those elements. |
| 63 | +public struct AsyncInterspersedSequence<Base: AsyncSequence> { |
| 64 | + @usableFromInline |
| 65 | + internal let base: Base |
| 66 | + |
| 67 | + @usableFromInline |
| 68 | + internal let separator: Base.Element |
| 69 | + |
| 70 | + @usableFromInline |
| 71 | + internal init(_ base: Base, separator: Base.Element) { |
| 72 | + self.base = base |
| 73 | + self.separator = separator |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension AsyncInterspersedSequence: AsyncSequence { |
| 78 | + public typealias Element = Base.Element |
| 79 | + |
| 80 | + /// The iterator for an `AsyncInterspersedSequence` asynchronous sequence. |
| 81 | + public struct AsyncIterator: AsyncIteratorProtocol { |
| 82 | + @usableFromInline |
| 83 | + internal enum State { |
| 84 | + case start |
| 85 | + case element(Result<Base.Element, Error>) |
| 86 | + case separator |
| 87 | + } |
| 88 | + |
| 89 | + @usableFromInline |
| 90 | + internal var iterator: Base.AsyncIterator |
| 91 | + |
| 92 | + @usableFromInline |
| 93 | + internal let separator: Base.Element |
| 94 | + |
| 95 | + @usableFromInline |
| 96 | + internal var state = State.start |
| 97 | + |
| 98 | + @usableFromInline |
| 99 | + internal init(_ iterator: Base.AsyncIterator, separator: Base.Element) { |
| 100 | + self.iterator = iterator |
| 101 | + self.separator = separator |
| 102 | + } |
| 103 | + |
| 104 | + public mutating func next() async rethrows -> Base.Element? { |
| 105 | + // After the start, the state flips between element and separator. Before |
| 106 | + // returning a separator, a check is made for the next element as a |
| 107 | + // separator is only returned between two elements. The next element is |
| 108 | + // stored to allow it to be returned in the next iteration. However, if |
| 109 | + // the checking the next element throws, the separator is emitted before |
| 110 | + // rethrowing that error. |
| 111 | + switch state { |
| 112 | + case .start: |
| 113 | + state = .separator |
| 114 | + return try await iterator.next() |
| 115 | + case .separator: |
| 116 | + do { |
| 117 | + guard let next = try await iterator.next() else { return nil } |
| 118 | + state = .element(.success(next)) |
| 119 | + } catch { |
| 120 | + state = .element(.failure(error)) |
| 121 | + } |
| 122 | + return separator |
| 123 | + case .element(let result): |
| 124 | + state = .separator |
| 125 | + return try result._rethrowGet() |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @inlinable |
| 131 | + public func makeAsyncIterator() -> AsyncInterspersedSequence<Base>.AsyncIterator { |
| 132 | + AsyncIterator(base.makeAsyncIterator(), separator: separator) |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
0 commit comments