On this page
ControlNotificationStream.flatMap(_:)
- Declared in
- ControlNotificationStream
- Package
- libtmux-swift
- flatMap(_:) ( transform : (Self.Element) async throws -> SegmentOfResult ) AsyncThrowingFlatMapSequence<Self, SegmentOfResult> ( transform : (Self.Element) async -> SegmentOfResult ) AsyncFlatMapSequence<Self, SegmentOfResult> ( transform : (Self.Element) async -> SegmentOfResult ) AsyncFlatMapSequence<Self, SegmentOfResult> ( transform : (Self.Element) async -> SegmentOfResult ) AsyncFlatMapSequence<Self, SegmentOfResult>
-
Creates an asynchronous sequence that concatenates the results of calling the given error-throwing transformation with each element of this sequence.
Use this method to receive a single-level asynchronous sequence when your transformation produces an asynchronous sequence for each element.
In this example, an asynchronous sequence called
CounterproducesIntvalues from1to5. The transforming closure takes the receivedIntand returns a newCounterthat counts that high. For example, when the transform receives3from the base sequence, it creates a newCounterthat produces the values1,2, and3. TheflatMap(_:)method "flattens" the resulting sequence-of-sequences into a singleAsyncSequence. However, when the closure receives4, it throws an error, terminating the sequence.do { let stream = Counter(howHigh: 5) .flatMap { (value) -> Counter in if value == 4 { throw MyError() } return Counter(howHigh: value) } for try await number in stream { print(number, terminator: " ") } } catch { print(error) } // Prints "1 1 2 1 2 3 MyError() "
- Parameter transform: An error-throwing mapping closure.
transformaccepts an element of this sequence as its parameter and returns anAsyncSequence. Iftransformthrows an error, the sequence ends. - Returns: A single, flattened asynchronous sequence that contains all elements in all the asynchronous sequences produced by
transform. The sequence ends either when the last sequence created from the last element from base sequence ends, or whentransformthrows an error.
- Parameter transform: An error-throwing mapping closure.
0 declared, 0 inherited