Structures

The following structures are available globally.

  • An async sequence that performs type erasure by wrapping another async sequence.

    If the async sequence that you wish to type erase can throw, then use AnyThrowingAsyncSequenceable.

    See more

    Declaration

    Swift

    public struct AnyAsyncSequenceable<Element> : AsyncSequence
    extension AnyAsyncSequenceable: AsyncIteratorProtocol
  • A throwing async sequence that performs type erasure by wrapping another throwing async sequence.

    If the async sequence that you wish to type erase doesn’t throw, then use AnyAsyncSequenceable.

    See more

    Declaration

    Swift

    public struct AnyThrowingAsyncSequenceable<Element> : AsyncSequence
    extension AnyThrowingAsyncSequenceable: AsyncIteratorProtocol
  • An asynchronous sequence that combines three async sequences.

    The combined sequence emits a tuple of the most-recent elements from each sequence when any of them emit a value.

    If one sequence never emits a value this sequence will finish.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(2)
        continuation.yield(3)
        continuation.yield(4)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(5)
        continuation.yield(6)
        continuation.yield(7)
        continuation.yield(8)
        continuation.yield(9)
        continuation.finish()
    }
    
    let streamC = .init { continuation in
        continuation.yield(10)
        continuation.yield(11)
        continuation.finish()
    }
    
    for await value in streamA.combineLatest(streamB, streamC) {
        print(value)
    }
    
    // Prints:
    // (1, 5, 10)
    // (2, 6, 11)
    // (3, 7, 11)
    // (4, 8, 11)
    // (4, 9, 11)
    
    See more

    Declaration

    Swift

    public struct CombineLatest3AsyncSequence<P, Q, R> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence, R : AsyncSequence
    extension CombineLatest3AsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that combines two async sequences.

    The combined sequence emits a tuple of the most-recent elements from each sequence when any of them emit a value.

    If one sequence never emits a value this sequence will finish.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(2)
        continuation.yield(3)
        continuation.yield(4)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(5)
        continuation.yield(6)
        continuation.yield(7)
        continuation.yield(8)
        continuation.yield(9)
        continuation.finish()
    }
    
    for await value in streamA.combineLatest(streamB) {
        print(value)
    }
    
    // Prints:
    // (1, 5)
    // (2, 6)
    // (3, 7)
    // (4, 8)
    // (4, 9)
    
    See more

    Declaration

    Swift

    public struct CombineLatestAsyncSequence<P, Q> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence
    extension CombineLatestAsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that immediately throws an error when iterated.

    let stream = Fail<Int, TestError>(error: TestError())
    
    do {
        for try await value in stream {
            print(value)
        }
    } catch {
        print("Error!")
    }
    
    // Prints:
    // Error!
    
    See more

    Declaration

    Swift

    public struct Fail<Element, Failure> : AsyncSequence where Failure : Error
    extension Fail: AsyncIteratorProtocol
  • An asynchronous sequence that only emits the provided value once.

    let stream = Just(1)
    
    for await value in stream {
        print(value)
    }
    
    // Prints:
    // 1
    
    See more

    Declaration

    Swift

    public struct Just<Element> : AsyncSequence
    extension Just: AsyncIteratorProtocol
  • An asynchronous sequence that merges three async sequences.

    The sequences are iterated through in parallel.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(4)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(2)
        continuation.finish()
    }
    
    let streamC = .init { continuation in
        continuation.yield(3)
        continuation.finish()
    }
    
    for await value in self.streamA.merge(with: self.streamB, self.streamC) {
        print(value)
    }
    
    // Prints:
    // 1
    // 2
    // 3
    // 4
    
    See more

    Declaration

    Swift

    public struct Merge3AsyncSequence<T> : AsyncSequence where T : AsyncSequence
    extension Merge3AsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that merges two async sequences.

    The sequences are iterated through in parallel.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(2)
        continuation.yield(3)
        continuation.yield(4)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(5)
        continuation.yield(6)
        continuation.yield(7)
        continuation.yield(8)
        continuation.yield(9)
        continuation.finish()
    }
    
    for await value in streamA.merge(with: streamB) {
        print(value)
    }
    
    // Prints:
    // 1
    // 5
    // 2
    // 6
    // 3
    // 7
    // 4
    // 8
    // 9
    
    See more

    Declaration

    Swift

    public struct MergeAsyncSequence<T> : AsyncSequence where T : AsyncSequence
    extension MergeAsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that streams only elements from the base asynchronous sequence that don’t match the previous element.

    let stream = .init { continuation in
        continuation.yield(1)
        continuation.yield(1)
        continuation.yield(2)
        continuation.yield(3)
        continuation.finish()
    }
    
    for await value in stream.removeDuplicates() {
        print(value)
    }
    
    // Prints:
    // 1
    // 2
    // 3
    
    See more

    Declaration

    Swift

    public struct RemoveDuplicatesAsyncSequence<Base> : AsyncSequence where Base : AsyncSequence, Base.Element : Equatable
    extension RemoveDuplicatesAsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that applys a zip function to the three async sequences.

    Use Zip3AsyncSequence to combine the latest elements from three async sequcnes and emit a tuple.

    The async sequence waits until both provided async sequences have emitted an element, then emits both elements as a tuple.

    If one sequence never emits a value or raises an error then the zipped sequence will finish.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(2)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(5)
        continuation.yield(6)
        continuation.yield(7)
        continuation.finish()
    }
    
    let streamC = .init { continuation in
        continuation.yield(8)
        continuation.yield(9)
        continuation.finish()
    }
    
    for await value in streamA.zip(streamB, streamC) {
        print(value)
    }
    
    // Prints:
    // (1, 5, 8)
    // (2, 6, 9)
    
    See more

    Declaration

    Swift

    public struct Zip3AsyncSequence<P, Q, R> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence, R : AsyncSequence
    extension Zip3AsyncSequence: AsyncIteratorProtocol
  • An asynchronous sequence that applys a zip function to the two async sequences.

    Use ZipAsyncSequence to combine the latest elements from two async sequences and emit a tuple.

    The async sequence waits until both provided async sequences have emitted an element, then emits both elements as a tuple.

    If one sequence never emits a value or raises an error then the zipped sequence will finish.

    let streamA = .init { continuation in
        continuation.yield(1)
        continuation.yield(2)
        continuation.finish()
    }
    
    let streamB = .init { continuation in
        continuation.yield(5)
        continuation.yield(6)
        continuation.yield(7)
        continuation.finish()
    }
    
    for await value in streamA.zip(streamB) {
        print(value)
    }
    
    // Prints:
    // (1, 5)
    // (2, 6)
    
    See more

    Declaration

    Swift

    public struct ZipAsyncSequence<P, Q> : AsyncSequence where P : AsyncSequence, Q : AsyncSequence
    extension ZipAsyncSequence: AsyncIteratorProtocol