- Author(s): Susan Cheng
Produces a sequence containing the original sequence followed by recursive mapped sequence.
struct View {
var id: Int
var children: [View] = []
}
let tree = [
View(id: 1, children: [
View(id: 3),
View(id: 4, children: [
View(id: 6),
]),
View(id: 5),
]),
View(id: 2),
]
for await view in tree.async.recursiveMap({ $0.children.async }) {
print(view.id)
}
// 1
// 2
// 3
// 4
// 5
// 6
The recursiveMap(_:)
method is declared as AsyncSequence
extensions, and return AsyncRecursiveMapSequence
or AsyncThrowingRecursiveMapSequence
instance:
extension AsyncSequence {
public func recursiveMap<S>(
_ transform: @Sendable @escaping (Element) async -> S
) -> AsyncRecursiveMapSequence<Self, S>
public func recursiveMap<S>(
_ transform: @Sendable @escaping (Element) async throws -> S
) -> AsyncThrowingRecursiveMapSequence<Self, S>
}
Calling this method is O(1).