嘗試運行名爲「Beginning Scala」的Apress書中的示例代碼。我甚至從他們的網站下載了代碼,以確保我沒有混淆。獲得以下消息:「Beginning Scala」代碼示例中的代碼錯誤
/root/sum.scala:19: error: missing arguments for method collect in trait Iterator;
follow this method with `_' if you want to treat it as a partially applied function
val lines = input.getLines.collect
^
one error found
,這裏是我使用(運行斯卡拉版2.8.1.final(Java的熱點(TM)服務器VM,在Fedora 13)的Java 1.6.0_22
import scala.io._
def toInt(in: String): Option[Int] =
try {
Some(Integer.parseInt(in.trim))
} catch {
case e: NumberFormatException => None
}
def sum(in: Seq[String]) = {
val ints = in.flatMap(s => toInt(s))
ints.foldLeft(0)((a, b) => a + b)
}
println("Enter some numbers and press ctrl-D (Unix/Mac) ctrl-C (Windows)")
val input = Source.fromInputStream(System.in)
val lines = input.getLines.collect
println("Sum "+sum(lines))
源代碼
看起來這是相關的變化:。
在2.7.7回報序列的Iterator.collect()方法在2.8,它是用來使用PartialFunction執行條件地圖你可以使用input.getLines.toSeq代替。
這是錯的太多的方法讓我通過我的電話做出迴應。作爲一個快速和骯髒的修復,嘗試簡單地刪除該行末尾的'.collect'。 – 2011-05-17 22:46:51