Kotlin is so beautiful :kotlinnew:
# random
p
Kotlin is so beautiful K
❤️ 5
e
Copy code
private suspend fun SequenceScope<DocumentFile>.walk(root: DocumentFile) {
    yield(root)
    if (root.isDirectory) {
        for (child in root.listFiles()) {
            walk(child)
        }
    }
}
fun DocumentFile.walk() = sequence {
    walk(this@walk)
}
p
Why are you writing this?
e
hit the wrong enter which posted early
but I'd rather use a single sequence scope than create many subsequences - still uses stack per recursive level, but less
p
Ah, good idea 💡 Here the same within a single function:
Copy code
fun DocumentFile.walk(): Sequence<DocumentFile> {
  return sequence {
    suspend fun SequenceScope<DocumentFile>.walk(file: DocumentFile) {
      yield(file)
      if (file.isDirectory) {
        file.listFiles().forEach {
          walk(it)
        }
      }
    }
    walk(this@walk)
  }
}