-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathAsyncThrowingRecursiveMapSequence.swift
127 lines (103 loc) · 4.07 KB
/
AsyncThrowingRecursiveMapSequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
extension AsyncSequence {
/// Returns 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
/// ```
///
/// - Parameters:
/// - transform: A closure that map the element to new sequence.
/// - Returns: A sequence of the original sequence followed by recursive mapped sequence.
@inlinable
public func recursiveMap<C>(_ transform: @Sendable @escaping (Element) async throws -> C) -> AsyncThrowingRecursiveMapSequence<Self, C> {
return AsyncThrowingRecursiveMapSequence(self, transform)
}
}
public struct AsyncThrowingRecursiveMapSequence<Base: AsyncSequence, Transformed: AsyncSequence>: AsyncSequence where Base.Element == Transformed.Element {
public typealias Element = Base.Element
@usableFromInline
let base: Base
@usableFromInline
let transform: @Sendable (Base.Element) async throws -> Transformed
@inlinable
init(_ base: Base, _ transform: @Sendable @escaping (Base.Element) async throws -> Transformed) {
self.base = base
self.transform = transform
}
@inlinable
public func makeAsyncIterator() -> AsyncIterator {
return AsyncIterator(base, transform)
}
}
extension AsyncThrowingRecursiveMapSequence {
public struct AsyncIterator: AsyncIteratorProtocol {
@usableFromInline
var base: Base.AsyncIterator?
@usableFromInline
var mapped: ArraySlice<Transformed> = []
@usableFromInline
var mapped_iterator: Transformed.AsyncIterator?
@usableFromInline
var transform: @Sendable (Base.Element) async throws -> Transformed
@inlinable
init(_ base: Base, _ transform: @Sendable @escaping (Base.Element) async throws -> Transformed) {
self.base = base.makeAsyncIterator()
self.transform = transform
}
@inlinable
public mutating func next() async throws -> Base.Element? {
if self.base != nil {
if let element = try await self.base?.next() {
try await mapped.append(transform(element))
return element
}
self.base = nil
self.mapped_iterator = mapped.popFirst()?.makeAsyncIterator()
}
while self.mapped_iterator != nil {
if let element = try await self.mapped_iterator?.next() {
try await mapped.append(transform(element))
return element
}
self.mapped_iterator = mapped.popFirst()?.makeAsyncIterator()
}
return nil
}
}
}
extension AsyncThrowingRecursiveMapSequence: Sendable
where Base: Sendable, Base.Element: Sendable, Transformed: Sendable { }
extension AsyncThrowingRecursiveMapSequence.AsyncIterator: Sendable
where Base.AsyncIterator: Sendable, Base.Element: Sendable, Transformed: Sendable, Transformed.AsyncIterator: Sendable { }