|
15 | 15 |
|
16 | 16 | ## Proposed Solution
|
17 | 17 |
|
| 18 | +### Grouping |
| 19 | + |
| 20 | +```swift |
| 21 | +extension AsyncSequence { |
| 22 | + @inlinable |
| 23 | + public func chunked<Collected: RangeReplaceableCollection>( |
| 24 | + by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool, |
| 25 | + collectedInto: Collected.Type |
| 26 | + ) -> AsyncChunkedByGroupSequence<Self, Collected> |
| 27 | + where Collected.Element == Element |
| 28 | + |
| 29 | + public func chunked( |
| 30 | + by belongInSameGroup: @escaping @Sendable (Element, Element) -> Bool |
| 31 | + ) -> AsyncChunkedByGroupSequence<Self, [Element]> |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### Projected Seperator |
| 36 | + |
| 37 | +```swift |
| 38 | +extension AsyncSequence { |
| 39 | + public func chunked<Subject : Equatable, Collected: RangeReplaceableCollection>( |
| 40 | + on projection: @escaping @Sendable (Element) -> Subject, |
| 41 | + collectedInto: Collected.Type |
| 42 | + ) -> AsyncChunkedOnProjectionSequence<Self, Subject, Collected> |
| 43 | + where Collected.Element == Element |
| 44 | + |
| 45 | + public func chunked<Subject : Equatable>( |
| 46 | + on projection: @escaping @Sendable (Element) -> Subject |
| 47 | + ) -> AsyncChunkedOnProjectionSequence<Self, Subject, [Element]> |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +### Count or Signal |
| 52 | + |
| 53 | +```swift |
| 54 | +extension AsyncSequence { |
| 55 | + public func chunked<Signal, Collected: RangeReplaceableCollection>( |
| 56 | + byCount count: Int, |
| 57 | + andSignal signal: Signal, |
| 58 | + collectedInto: Collected.Type |
| 59 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, Collected, Signal> |
| 60 | + where Collected.Element == Element |
| 61 | + |
| 62 | + public func chunked<Signal>( |
| 63 | + byCount count: Int, |
| 64 | + andSignal signal: Signal |
| 65 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, [Element], Signal> |
| 66 | + |
| 67 | + public func chunked<Signal, Collected: RangeReplaceableCollection>( |
| 68 | + bySignal signal: Signal, |
| 69 | + collectedInto: Collected.Type |
| 70 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, Collected, Signal> |
| 71 | + where Collected.Element == Element |
| 72 | + |
| 73 | + public func chunked<Signal>( |
| 74 | + bySignal signal: Signal) -> AsyncChunksOfCountAndSignalSequence<Self, [Element], Signal> |
| 75 | + |
| 76 | + public func chunked<C: Clock, Collected: RangeReplaceableCollection>( |
| 77 | + byCount count: Int, |
| 78 | + andTime timer: AsyncTimerSequence<C>, |
| 79 | + collectedInto: Collected.Type |
| 80 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, Collected, AsyncTimerSequence<C>> |
| 81 | + where Collected.Element == Element |
| 82 | + |
| 83 | + public func chunked<C: Clock>( |
| 84 | + byCount count: Int, |
| 85 | + andTime timer: AsyncTimerSequence<C> |
| 86 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, [Element], AsyncTimerSequence<C>> |
| 87 | + |
| 88 | + public func chunked<C: Clock, Collected: RangeReplaceableCollection>( |
| 89 | + byTime timer: AsyncTimerSequence<C>, |
| 90 | + collectedInto: Collected.Type |
| 91 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, Collected, AsyncTimerSequence<C>> |
| 92 | + where Collected.Element == Element |
| 93 | + |
| 94 | + public func chunked<C: Clock>( |
| 95 | + byTime timer: AsyncTimerSequence<C> |
| 96 | + ) -> AsyncChunksOfCountAndSignalSequence<Self, [Element], AsyncTimerSequence<C>> |
| 97 | +} |
| 98 | + |
| 99 | +extension AsyncSequence { |
| 100 | + public func chunks<Collected: RangeReplaceableCollection>( |
| 101 | + ofCount count: Int, |
| 102 | + collectedInto: Collected.Type |
| 103 | + ) -> AsyncChunksOfCountSequence<Self, [Element]> |
| 104 | + where Collected.Element == Element |
| 105 | + |
| 106 | + public func chunks( |
| 107 | + ofCount count: Int |
| 108 | + ) -> AsyncChunksOfCountSequence<Self, [Element]> |
| 109 | +} |
| 110 | +``` |
| 111 | + |
18 | 112 | ## Detailed Design
|
19 | 113 |
|
| 114 | +### Grouping |
| 115 | + |
| 116 | +```swift |
| 117 | +public struct AsyncChunkedByGroupSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence |
| 118 | + where Collected.Element == Base.Element { |
| 119 | + public typealias Element = Collected { |
| 120 | + |
| 121 | + public struct Iterator: AsyncIteratorProtocol { |
| 122 | + public mutating func next() async rethrows -> Collected? |
| 123 | + } |
| 124 | + |
| 125 | + public func makeAsyncIterator() -> Iterator |
| 126 | +} |
| 127 | + |
| 128 | +extension AsyncChunkedByGroupSequence: Sendable |
| 129 | + where Base: Sendable, Base.Element: Sendable { } |
| 130 | + |
| 131 | +extension AsyncChunkedByGroupSequence.Iterator: Sendable |
| 132 | + where Base.AsyncIterator: Sendable, Base.Element: Sendable { } |
| 133 | +``` |
| 134 | + |
| 135 | +### Projected Seperator |
| 136 | + |
| 137 | +```swift |
| 138 | +public struct AsyncChunkedOnProjectionSequence<Base: AsyncSequence, Subject: Equatable, Collected: RangeReplaceableCollection>: AsyncSequence where Collected.Element == Base.Element { |
| 139 | + public typealias Element = (Subject, Collected) |
| 140 | + |
| 141 | + public struct Iterator: AsyncIteratorProtocol { |
| 142 | + public mutating func next() async rethrows -> (Subject, Collected)? |
| 143 | + } |
| 144 | + |
| 145 | + public func makeAsyncIterator() -> Iterator |
| 146 | +} |
| 147 | + |
| 148 | +extension AsyncChunkedOnProjectionSequence: Sendable |
| 149 | + where Base: Sendable, Base.Element: Sendable { } |
| 150 | +extension AsyncChunkedOnProjectionSequence.Iterator: Sendable |
| 151 | + where Base.AsyncIterator: Sendable, Base.Element: Sendable, Subject: Sendable { } |
| 152 | +``` |
| 153 | + |
| 154 | +### Count |
| 155 | + |
| 156 | +```swift |
| 157 | +public struct AsyncChunksOfCountSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection>: AsyncSequence |
| 158 | + where Collected.Element == Base.Element { |
| 159 | + public typealias Element = Collected |
| 160 | + |
| 161 | + public struct Iterator: AsyncIteratorProtocol { |
| 162 | + public mutating func next() async rethrows -> Collected? |
| 163 | + } |
| 164 | + |
| 165 | + public func makeAsyncIterator() -> Iterator |
| 166 | +} |
| 167 | + |
| 168 | +extension AsyncChunksOfCountSequence : Sendable where Base : Sendable, Base.Element : Sendable { } |
| 169 | +extension AsyncChunksOfCountSequence.Iterator : Sendable where Base.AsyncIterator : Sendable, Base.Element : Sendable { } |
| 170 | + |
| 171 | +``` |
| 172 | + |
| 173 | +### Count or Signal |
| 174 | + |
| 175 | +```swift |
| 176 | +public struct AsyncChunksOfCountAndSignalSequence<Base: AsyncSequence, Collected: RangeReplaceableCollection, Signal: AsyncSequence>: AsyncSequence, Sendable |
| 177 | + where |
| 178 | + Collected.Element == Base.Element, |
| 179 | + Base: Sendable, Signal: Sendable, |
| 180 | + Base.AsyncIterator: Sendable, Signal.AsyncIterator: Sendable, |
| 181 | + Base.Element: Sendable, Signal.Element: Sendable { |
| 182 | + public typealias Element = Collected |
| 183 | + |
| 184 | + public struct Iterator: AsyncIteratorProtocol, Sendable { |
| 185 | + public mutating func next() async rethrows -> Collected? |
| 186 | + } |
| 187 | + |
| 188 | + public func makeAsyncIterator() -> Iterator |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +## Alternatives Considered |
| 193 | + |
20 | 194 | ## Credits/Inspiration
|
21 | 195 |
|
| 196 | +This transformation function is a direct analog to the synchronous version [defined in the Swift Algorithms package](https://github.com/apple/swift-algorithms/blob/main/Guides/Chunked.md) |
0 commit comments