2011-08-21 35 views
3
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.length) 
    cleaned.foreach(println(_)) 
    println("**" + cleaned.length) 
    cleaned.foreach(masterList.append(_)) 
} 

從代碼的輸出如下爲什麼我的Scala列表在下面的代碼中消失?

*175 
**0 

我不明白爲什麼我的列表中消失

回答

18

cleaned是一個迭代器。

scala> val cleaned = lines.filter(_!="") 
cleaned: Iterator[String] = non-empty iterator 

分配它之後不是空的。 Scala的迭代器是單使用 - 一旦你經過它(例如,通過應用length法)成爲空:

scala> cleaned.length 
res0: Int = 13 

scala> cleaned.length 
res1: Int = 0 

您可通過以下方法列出修復行爲,或SEQ(懶惰):

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 
+0

哇。這種行爲讓我無法理解。從來沒有想過,對'長度'的無害調用實際上會破壞整個事情! – lolski

+0

@ lolski我會說*消耗*整個事情 –

+0

這就是更好的詞。 omn​​omnomnomm – lolski

6

Source.fromFile(new File("data\\" + file), "UTF-16").getLines返回Iterator[String]。篩選仍會返回Iterator。在迭代器上調用length將導致它被完全讀取,直到hasNext返回false。第二次,它是空的。類似於:

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 
相關問題