Skip to content

Latest commit

 

History

History
56 lines (46 loc) · 1.33 KB

RecursiveMap.md

File metadata and controls

56 lines (46 loc) · 1.33 KB

RecursiveMap

[ Source | Tests ]

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

Detailed Design

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>
}

Complexity

Calling this method is O(1).