Clean way of reading all input lines in Kotlin
Asked Answered
N

3

6

A common pattern when doing coding challenges is to read many lines of input. Assuming you don't know in advance how many lines, you want to read until EOF (readLine returns null).

Also as a preface, I don't want to rely on java.utils.* since I'm coding in KotlinNative, so no Scanner.

I would like to maybe do something like

val lines = arrayListOf<String>()
for (var line = readLine(); line != null; line = readLine()) {
    lines.add(line)
}

But that clearly isn't valid Kotlin. The cleanest I can come up with is:

while (true) {
    val line = readLine()
    if (line == null) break
    lines.add(line)
}

This works, but it just doesn't seem very idiomatic. Is there a better way to read all lines into an array, without using a while/break loop?

Nesta answered 1/12, 2018 at 21:8 Comment(0)
C
14

generateSequence has the nice property that it will complete if the internal generator returns null and accepts only a single iteration, so the following code could be valid:

val input = generateSequence(::readLine)
val lines = input.toList()

Then like s1m0nw1's answer you can use any of the available Sequence<String> methods to refine this as desired for your solution.

Chism answered 1/12, 2018 at 22:46 Comment(2)
generateSequence(::readLine) is clean and idiomatic, just what I was looking for. Thank you!Nesta
Note that this doesn't work for redirected input in Kotlin Native (see youtrack.jetbrains.com/issue/KT-39495). You can instead use generateSequence(::readlnOrNull)Gunplay
D
6

I guess you're talking about reading from System.in (stdin) here. You could make that work with sequences:

val lines = generateSequence(readLine()) {
    readLine()
}

lines.take(5).forEach { println("read: $it") }

We begin our sequence with a first readLine (the sequence's seed) and then read the next line until null is encountered. The sequence is possibly infinite, therefore we just take the first five inputs in the example. Read about details on Sequence here.

Direct answered 1/12, 2018 at 21:55 Comment(0)
B
0

Most likely, you want to loop through them. As a more complicated example, consider that you want to call reader.readLine() continuously, and you want both the value and the index of the value:

generateSequence(reader::readLine).mapIndexed { index, line ->
    // Do stuff
}
Bricklaying answered 24/5, 2023 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.