all but the last item from a Scala Iterator (a.k.a. Iterator.init)

Viewed 8820

I have a number of large files where I want to process all but the last line in each file. If the files were small, I could just convert to a TraversableLike and use the "init" method, e.g.:

lines.toList.init

But the files are large so I need to keep things as an iterator. Is there a simple way to get something like "init" on an Iterator? I'm considering the following, but I'm not confident it will always work:

lines.takeWhile(_ => lines.hasNext)
1 Answers
Related