How do I define a sequence that enumerates all ancestors of a node in a tree (nodes have multiple parents)? Code in ๐งต
mcpiroman
08/27/2021, 1:06 PM
My try is the below, but it produces a `Suspension functions can be called only within coroutine body`error on the `yieldAll`call.
Copy code
fun allElementAncestors() = sequence {
fun visitParents(visited: Element) {
yieldAll(visited.elementParents)
for (parent in visited.elementParents) {
visitParents(parent.element)
}
}
visitParents(this@Element)
}
d
Dominaezzz
08/27/2021, 2:11 PM
Make visit parents a suspend function
m
mcpiroman
08/27/2021, 2:13 PM
Tried that too, but this yields an `Restricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope`error on `yieldAll`and
visitParents(this@Element)
d
Dominaezzz
08/27/2021, 2:28 PM
You have to annotate it with the restricts suspension annotation. The same one yield all has