說我有一些Scala代碼是這樣的:爲什麼我不能在for-yield表達式上調用方法?
// Outputs 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
println(squares)
def squares = {
val s = for (count <- 1 to 10)
yield { count * count }
s.mkString(", ");
}
爲什麼我不得不使用臨時VAL S'我嘗試這樣做:
def squares = for (count <- 1 to 10)
yield { count * count }.mkString(", ")
失敗,出現此錯誤消息編譯:
error: value mkString is not a member of Int
def squares = for (count <- 1 to 10) yield { count * count }.mkString(", ")
不宜mkString
被稱爲由for
環路返回的集合?
他的理解是,你所提供的完全一樣的地圖方法只是語法糖。在我看來,你想使用的僅僅是品味的問題。 – 2010-06-02 14:42:25