is there a better way to write this in Kotlin? I'm...
# getting-started
m
is there a better way to write this in Kotlin? I'm not trying to solve anything real, just playing around
Copy code
fun File.asSequence(): Sequence<String> {
    val reader = this.bufferedReader()
    return sequence {
        while (true) {
            yield(reader.readLine() ?: break)
        }
    }
}
and is combining scopes frown upon? for example
Copy code
fun File.asSequence2(): Sequence<String> = this.bufferedReader().let {
  // now sequence { } allows me to have `it` for the reader and `this` for the sequence
}
(ignore the fact that that first line in particular got really long)
t
You could do the same thing with the
useLines
extension function from the stdlib: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.io/java.io.-file/use-lines.html As for your 2nd question, I see nothing bad in mixing
it
and
this
as long as it is still obvious which refers to what.
đź‘Ť 2