Skip to content

Commit

Permalink
Replace chain operator with a function (#26)
Browse files Browse the repository at this point in the history
* Try to make operator less ambiguous

* Remove the operator and replace with #chain
  • Loading branch information
reddavis authored Apr 11, 2022
1 parent 5bb2482 commit 57e93c5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 62 deletions.
105 changes: 50 additions & 55 deletions Asynchrone/Source/Sequences/ChainAsyncSequenceable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> { continuation in
/// continuation.yield(1)
/// continuation.yield(2)
/// continuation.yield(3)
/// continuation.finish()
/// }
///
/// let sequenceB = AsyncStream<Int> { continuation in
/// continuation.yield(4)
/// continuation.yield(5)
/// continuation.yield(6)
/// continuation.finish()
/// }
///
/// let sequenceC = AsyncStream<Int> { 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 <><P, Q>(
lhs: P,
rhs: Q
) -> ChainAsyncSequence<P, Q> 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<Int> { continuation in
/// continuation.yield(1)
/// continuation.yield(2)
/// continuation.yield(3)
/// continuation.finish()
/// }
///
/// let sequenceB = AsyncStream<Int> { continuation in
/// continuation.yield(4)
/// continuation.yield(5)
/// continuation.yield(6)
/// continuation.finish()
/// }
///
/// let sequenceC = AsyncStream<Int> { 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<P>(with sequence: P) -> ChainAsyncSequence<Self, P> where P: AsyncSequence {
.init(self, sequence)
}
}
11 changes: 5 additions & 6 deletions AsynchroneTests/Tests/Sequences/ChainAsyncSequenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ let sequenceC = AsyncStream<Int> { continuation in
continuation.finish()
}

for await value in sequenceA <> sequenceB <> sequenceC {
for await value in sequenceA.chain(with: sequenceB).chain(with: sequenceC) {
print(value)
}

Expand Down

0 comments on commit 57e93c5

Please sign in to comment.