【发布时间】:2011-05-19 18:39:42
【问题描述】:
在Programming in Scala7.8 Refactoring imperative-style code:
// Returns a row as a sequence
def makeRowSeq(row: Int) =
for (col <- 1 to 10) yield {
val prod = (row * col).toString
val padding = " " * (4 - prod.length)
padding + prod
}
// Returns a row as a string
def makeRow(row: Int) = makeRowSeq(row).mkString
// Returns table as a string with one row per line
def multiTable() = {
val tableSeq = // a sequence of row strings
for (row <- 1 to 10)
yield makeRow(row)
tableSeq.mkString("\n")
}
看来yield makeRow(row)不知何故使用makeRowSeq 的“将行作为字符串返回”版本。这是怎么发生的?
【问题讨论】:
标签: scala yield implicit-conversion