说我有一些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