If I process the input from stdin
with scanLeft
, the resulting output is always one line behind my last input:
io.Source.stdin
.getLines
.scanLeft("START:")((accu, line) => accu + " " + line)
.foreach(println(_))
Results in (my manual inputs are preceded by >
):
> first
START:
> second
START: first
> third
START: first second
The sensible output I want is:
> first
START: first
> second
START: first second
> third
START: first second third
As you can see, the output following the first input line should already contain the string of the first input line.
I already tried it using .scanLeft(...).drop(1).foreach(...)
, but this leads to the following result:
> first
> second
START: first
> third
START: first second
How do I correctly omit the pure seed to get the desired result?
[UPDATE] For the time being I am content with Andrey Tyukin's nifty workaround. Many thanks for suggesting it.
But of course, if there is any alternative to scanLeft
that does not send the seed as first item into the following iteration chain, I will prefer that solution.
[UPDATE]
User jwvh understood my objective and provided an excellent solution to it. To round off their suggestion I seek a way of preprocessing the lines before sending them into the accumulation callback. Thus the readLine
command should not be called in the accumulation callback but in a different chain link I can prepend.