Ellen Spertus
09/29/2022, 2:19 PMLuke
09/29/2022, 2:25 PMfun readInt() : Int {
return generateSequence { readLine() }
.mapNotNull { it?.toIntOrNull() }
.first()
}
ephemient
09/29/2022, 2:46 PM.mapNotNull { ... }.first() == .firstNotNullOf { ... }
ephemient
09/29/2022, 2:47 PMreadLine()
returns null, that means you're at EOF and it will continue returning nullephemient
09/29/2022, 2:48 PMreadln()
function, it'll instead throw an exception on EOF insteadKlitos Kyriacou
09/29/2022, 4:43 PMreadLine()
should be regarded as deprecated - if you still want its behaviour (instead of readln()
), call readlnOrNull()
which is a much more descriptive name.Ellen Spertus
09/29/2022, 4:47 PMreadLine()
be regarded as deprecated? @Klitos KyriacouEllen Spertus
09/29/2022, 4:48 PMKlitos Kyriacou
09/29/2022, 4:50 PMreadlnOrNull()
does exactly the same thing as readLine()
(so you can do a global search and replace) but has a more descriptive name.Klitos Kyriacou
09/29/2022, 4:56 PMmapNotNull
etc would presumably also be too advanced at this stage. I would keep your function definition as it is, but replace readLine()?.toIntOrNull()
with readln().toIntOrNull()
.Klitos Kyriacou
09/30/2022, 8:08 AMreadln()
instead of readlnOrNull()
, it will throw an exception if you enter ^D (or ^Z for those who use Windows), which is probably better than an infinite loop.ephemient
09/30/2022, 1:12 PMreadln()
and abort. that's what I was trying to hint at earlier