Alternative title: how does one "nicely" "update" the contents of text files with Kotlin?
I have seen how do i write to a file in kotlin.
Basically, the suggested solution there is based on creating a File object, to then write to it: File("somefile.txt").printWriter().use ...
But well: I already have code like this:
File(fname).bufferedReader().lineSequence().flatMap { transform(it) }
where transform()
is my own method that either returns the original line, or some updates lines when some specific match matched. (its signature is transform(line : String) : Sequence<String>
)
Sure: I could assign the result of flatMap()
to some temporary variable, to then use that with printWriter().use
.
But is there a nice, canonical way that works without a temporary variable?