AsyncSequence

extension AsyncSequence
extension AsyncSequence where Element: Equatable

Erasure

  • Creates a throwing type erasing async sequence.

    If the async sequence that you wish to type erase deson’t throw, then use eraseToAnyAsyncSequenceable().

    Declaration

    Swift

    public func eraseToAnyThrowingAsyncSequenceable() -> AnyThrowingAsyncSequenceable<Element>

    Return Value

    A typed erased async sequence.

  • first() Asynchronous

    The first element of the sequence, if there is one.

    Declaration

    Swift

    public func first() async rethrows -> Element?
  • collect() Asynchronous

    Collect all elements from a sequence.

    Declaration

    Swift

    public func collect() async rethrows -> [Element]

    Return Value

    An array of all elements.

Combine latest

  • Combine 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)
    

    Declaration

    Swift

    public func combineLatest<Q, R>(
        _ q: Q,
        _ r: R
    ) -> CombineLatest3AsyncSequence<Self, Q, R> where Q: AsyncSequence, R: AsyncSequence

    Parameters

    q

    Another async sequence to combine with.

    r

    Another async sequence to combine with.

    Return Value

    A async sequence combines elements from all sequences.

  • Combine with an additional async sequence to produce a AsyncCombineLatest2Sequence.

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

    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 self.streamA.combineLatest(self.streamB) {
        print(value)
    }
    
    // Prints:
    // (1, 5)
    // (2, 6)
    // (3, 7)
    // (4, 8)
    // (4, 9)
    

    Declaration

    Swift

    public func combineLatest<Q>(
        _ other: Q
    ) -> CombineLatestAsyncSequence<Self, Q> where Q: AsyncSequence

    Parameters

    other

    Another async sequence to combine with.

    Return Value

    A async sequence combines elements from this and another async sequence.

Merge

  • 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 streamA.merge(with: streamB, streamC) {
        print(value)
    }
    
    // Prints:
    // 1
    // 2
    // 3
    // 4
    

    Declaration

    Swift

    public func merge(
        with q: Self,
        _ r: Self
    ) -> Merge3AsyncSequence<Self>

    Parameters

    q

    An async sequence.

    r

    An async sequence.

    Return Value

    A async sequence merges elements from this and another async sequence.

  • An asynchronous sequence that merges two async sequence.

    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
    

    Declaration

    Swift

    public func merge(
        with other: Self
    ) -> MergeAsyncSequence<Self>

    Parameters

    other

    Another async sequence to merge with.

    Return Value

    A async sequence merges elements from this and another async sequence.

Zip

  • Create an asynchronous sequence that applys a zip function to the three async sequences.

    Combines the latest elements from three async sequences and emits 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)
    

    Declaration

    Swift

    public func zip<Q, R>(
        _ q: Q,
        _ r: R
    ) -> Zip3AsyncSequence<Self, Q, R> where Q: AsyncSequence, R: AsyncSequence

    Parameters

    q

    Another async sequence.

    r

    Another async sequence.

    Return Value

    A async sequence zips elements from this and another async sequence.

  • Create an asynchronous sequence that applys a zip function to the two async sequences.

    Combines the latest elements from two async sequences and emits 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)
    

    Declaration

    Swift

    public func zip<Q>(
        _ other: Q
    ) -> ZipAsyncSequence<Self, Q> where Q: AsyncSequence

    Parameters

    other

    Another async sequence to zip with.

    Return Value

    A async sequence zips elements from this and another async sequence.

Available where Element: Equatable

  • Emits only elements that don’t match the previous element.

    Declaration

    Swift

    public func removeDuplicates() -> RemoveDuplicatesAsyncSequence<Self>

    Return Value

    A AsyncRemoveDuplicatesSequence instance.

  • Omits any element that the predicate determines is equal to the previous element.

    Declaration

    Swift

    public func removeDuplicates(
        by predicate: @escaping RemoveDuplicatesAsyncSequence<Self>.Predicate
    ) -> RemoveDuplicatesAsyncSequence<Self>

    Parameters

    predicate

    A closure to evaluate whether two elements are equivalent. Return true from this closure to indicate that the second element is a duplicate of the first.

    Return Value

    A AsyncRemoveDuplicatesSequence instance.