Skip to content

Commit 70cbd66

Browse files
committed
wip
1 parent c29b50a commit 70cbd66

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

Sources/Introspect.swift

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,11 @@ extension PlatformEntity {
7979
return nearestAncestor
8080
}
8181

82-
func descendantsBetween(_ bottomEntity: Base, and topEntity: Base) -> [Base] {
83-
var result: [Base] = []
84-
var entered = false
85-
86-
for descendant in self.allDescendants {
87-
if descendant === bottomEntity {
88-
entered = true
89-
} else if descendant === topEntity {
90-
break
91-
} else if entered {
92-
result.append(descendant)
93-
}
94-
}
95-
96-
return result
82+
func allDescendants(between bottomEntity: Base, and topEntity: Base) -> some Sequence<Base> {
83+
self.allDescendants
84+
.lazy
85+
.drop(while: { $0 !== bottomEntity })
86+
.prefix(while: { $0 !== topEntity })
9787
}
9888

9989
func receiver<PlatformSpecificEntity: PlatformEntity>(
@@ -102,14 +92,14 @@ extension PlatformEntity {
10292
) -> PlatformSpecificEntity? {
10393
let frontEntity = self
10494
guard
105-
let backEntity = Array(frontEntity.ancestors).last?.entityWithTag(anchorID.hashValue),
95+
let backEntity = ContiguousArray(frontEntity.ancestors).last?.entityWithTag(anchorID.hashValue),
10696
let commonAncestor = backEntity.nearestCommonAncestor(with: frontEntity~)
10797
else {
10898
return nil
10999
}
110100

111101
return commonAncestor
112-
.descendantsBetween(backEntity~, and: frontEntity~)
102+
.allDescendants(between: backEntity~, and: frontEntity~)
113103
.compactMap { $0 as? PlatformSpecificEntity }
114104
.first
115105
}

Sources/Utils.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
postfix operator ~
2+
3+
postfix func ~ <LHS, T>(lhs: LHS) -> T {
4+
lhs as! T
5+
}
6+
7+
postfix func ~ <LHS, T>(lhs: LHS?) -> T? {
8+
lhs as? T
9+
}
10+
11+
func recursiveSequence<S: Sequence>(_ sequence: S, children: @escaping (S.Element) -> S) -> AnySequence<S.Element> {
12+
AnySequence {
13+
var mainIterator = sequence.makeIterator()
14+
// Current generator, or `nil` if all sequences are exhausted:
15+
var iterator: AnyIterator<S.Element>?
16+
17+
return AnyIterator {
18+
guard let iterator, let element = iterator.next() else {
19+
if let element = mainIterator.next() {
20+
iterator = recursiveSequence(children(element), children: children).makeIterator()
21+
return element
22+
}
23+
return nil
24+
}
25+
return element
26+
}
27+
}
28+
}

Sources/Voodoo.swift

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)