Why does my Scala list disappear in the following code?
val files = new File("data").list()
val filtered = files.filter(name => name.contains("txn"))
val masterList = new ListBuffer[String]
for (file <- filtered) {
val lines = Source.fromFile(new File("data\\" + file), "UTF-16").getLines
val cleaned = lines.filter(!masterList.contains(_))
println("*" + cleaned.l开发者_开发知识库ength)
cleaned.foreach(println(_))
println("**" + cleaned.length)
cleaned.foreach(masterList.append(_))
}
The output from the code is as follows
*175
**0
I don't understand why my list disappears
cleaned
is an iterator.
scala> val cleaned = lines.filter(_!="")
cleaned: Iterator[String] = non-empty iterator
Right after assigning it is non-empty. Iterators in scala are single-used - once you traversed it (e.g. by applying length
method) it becomes empty:
scala> cleaned.length
res0: Int = 13
scala> cleaned.length
res1: Int = 0
You can fix that behavior by converting to List, or to Seq (lazy):
scala> val lines=Source.fromFile("bla.txt").getLines
lines: Iterator[String] = non-empty iterator
scala> val cleaned = lines.filter(_!="").toSeq
cleaned: Seq[String] = Stream(first, ?)
scala> cleaned.length
res4: Int = 13
scala> cleaned.length
res5: Int = 13
Source.fromFile(new File("data\\" + file), "UTF-16").getLines
returns an Iterator[String]
. Filtering still returns an Iterator
. Calling length
on an iterator will cause it to be read fully until hasNext
returns false. The second time around, it's empty. Similar to:
scala> val cleaned = List(1,2).iterator
cleaned: Iterator[Int] = non-empty iterator
scala> cleaned.length
res10: Int = 2
scala> cleaned.length
res11: Int = 0
精彩评论