https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

Manuel Pérez Alcolea

07/26/2020, 2:09 AM
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

tseisel

07/26/2020, 8:28 AM
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
2 Views