diff --git a/Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift b/Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift index 624f44a..e8fec4e 100644 --- a/Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift +++ b/Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift @@ -97,60 +97,55 @@ extension ChainAsyncSequence: AsyncIteratorProtocol { // MARK: Chain -precedencegroup ChainOperatorPrecedence { - associativity: left -} -infix operator <>: ChainOperatorPrecedence +extension AsyncSequence { -/// An asynchronous sequence that chains two async sequences. -/// -/// The combined sequence first emits the all the values from the first sequence -/// and then emits all values from the second. -/// -/// ```swift -/// let sequenceA = AsyncStream { continuation in -/// continuation.yield(1) -/// continuation.yield(2) -/// continuation.yield(3) -/// continuation.finish() -/// } -/// -/// let sequenceB = AsyncStream { continuation in -/// continuation.yield(4) -/// continuation.yield(5) -/// continuation.yield(6) -/// continuation.finish() -/// } -/// -/// let sequenceC = AsyncStream { continuation in -/// continuation.yield(7) -/// continuation.yield(8) -/// continuation.yield(9) -/// continuation.finish() -/// } -/// -/// for await value in sequenceA <> sequenceB <> sequenceC { -/// print(value) -/// } -/// -/// // Prints: -/// // 1 -/// // 2 -/// // 3 -/// // 4 -/// // 5 -/// // 6 -/// // 7 -/// // 8 -/// // 9 -/// ``` -/// - Parameters: -/// - lhs: The first async sequence to iterate through. -/// - rhs: The second async sequence to iterate through. -/// - Returns: A async sequence chains the two sequences. -public func <>( - lhs: P, - rhs: Q -) -> ChainAsyncSequence where P: AsyncSequence, Q: AsyncSequence { - .init(lhs, rhs) + /// An asynchronous sequence that chains two async sequences. + /// + /// The combined sequence first emits the all the values from the first sequence + /// and then emits all values from the second. + /// + /// ```swift + /// let sequenceA = AsyncStream { continuation in + /// continuation.yield(1) + /// continuation.yield(2) + /// continuation.yield(3) + /// continuation.finish() + /// } + /// + /// let sequenceB = AsyncStream { continuation in + /// continuation.yield(4) + /// continuation.yield(5) + /// continuation.yield(6) + /// continuation.finish() + /// } + /// + /// let sequenceC = AsyncStream { continuation in + /// continuation.yield(7) + /// continuation.yield(8) + /// continuation.yield(9) + /// continuation.finish() + /// } + /// + /// for await value in sequenceA.chain(with: sequenceB).chain(with: sequenceC) { + /// print(value) + /// } + /// + /// // Prints: + /// // 1 + /// // 2 + /// // 3 + /// // 4 + /// // 5 + /// // 6 + /// // 7 + /// // 8 + /// // 9 + /// ``` + /// - Parameters: + /// - lhs: The first async sequence to iterate through. + /// - rhs: The second async sequence to iterate through. + /// - Returns: A async sequence chains the two sequences. + public func chain

(with sequence: P) -> ChainAsyncSequence where P: AsyncSequence { + .init(self, sequence) + } } diff --git a/AsynchroneTests/Tests/Sequences/ChainAsyncSequenceTests.swift b/AsynchroneTests/Tests/Sequences/ChainAsyncSequenceTests.swift index a666d24..df3cff1 100644 --- a/AsynchroneTests/Tests/Sequences/ChainAsyncSequenceTests.swift +++ b/AsynchroneTests/Tests/Sequences/ChainAsyncSequenceTests.swift @@ -18,16 +18,15 @@ final class ChainAsyncSequenceTests: XCTestCase { // MARK: Tests func testChainingTwoSequences() async { - let values = await (self.sequenceA <> self.sequenceB).collect() + let values = await self.sequenceA.chain(with: self.sequenceB).collect() XCTAssertEqual(values, [1, 2, 3, 4, 5, 6]) } func testChainingThreeSequences() async { - let values = await ( - self.sequenceA <> - self.sequenceB <> - self.sequenceC - ).collect() + let values = await self.sequenceA + .chain(with: self.sequenceB) + .chain(with: self.sequenceC) + .collect() XCTAssertEqual(values, [1, 2, 3, 4, 5, 6, 7, 8, 9]) } diff --git a/README.md b/README.md index 9ed6e38..f960823 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ let sequenceC = AsyncStream { continuation in continuation.finish() } -for await value in sequenceA <> sequenceB <> sequenceC { +for await value in sequenceA.chain(with: sequenceB).chain(with: sequenceC) { print(value) }