Sam
01/29/2023, 4:23 PMdave08
01/29/2023, 4:39 PMuse { }
... thanks!hfhbd
01/29/2023, 4:42 PMSam
01/29/2023, 4:43 PMstreetsofboston
01/29/2023, 9:21 PMSam
01/29/2023, 9:25 PMephemient
01/30/2023, 7:35 PMflow {
emit(Unit)
TODO("unreachable")
}.first()
without as much warning as you do in normal code, but it basically works as expectedSam
01/30/2023, 7:54 PMephemient
01/30/2023, 7:59 PMsequence { ... }
is basically just
iterator { ... }.asSequence()
Sam
01/30/2023, 8:06 PMiterator { ... }
if they wanted to...ephemient
01/30/2023, 10:09 PMsequence
has been undoubtedly better than the alternatives for us. it is much more straightforward than writing a state machine for this
sequence {
val queue = ArrayDeque<T>().apply { add(start) }
while (queue.isNotEmpty()) {
val node = queue.removeFirst()
yield(node)
queue.addAll(node.children)
}
}
Sam
01/31/2023, 8:49 AMclass TreeIterator<T>(start: Node<T>): Iterator<Node<T>> {
private val queue = ArrayDeque<Node<T>>().apply { add(start) }
override fun hasNext(): Boolean = queue.isNotEmpty()
override fun next(): Node<T> {
val node = queue.removeFirst()
queue.addAll(node.children)
return node
}
}
But I do think your version is much more readable. That’s a really nice use of the sequence
builder and it makes the algorithm instantly recognisable.streetsofboston
01/31/2023, 4:00 PM