Pregunta

I have a big sequence of strings of which I am interested only in the portion that comes after finding a certain string. For example, the sequence could be -

..
..
one
two
three
four
five
..
..

And I want to filter all lines prior to four to have a filtered sequence containing only (four, five, ... and so on ...)

How can I write this in Scala in a functional way?

Thanks in advance.

¿Fue útil?

Solución

Is it stored in file or in memory in some kind of collection?

There is method dropWhile in all scala collections:

val s = Seq("..", "..", "one", "two", "three", "four", "five", "..", "..")
// Seq[String] = List(.., .., one, two, three, four, five, .., ..)

s.dropWhile{ _ != "four" }
// Seq[String] = List(four, five, .., ..)

It works for Iterator, so you could use it like this:

val lines = io.Source.fromFile("bigFile.txt").getLines().dropWhile{ _ != "four" }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top