我是Scala的新手,但对Haskell有一些经验。我做了以下操作:如何从Scala返回一个值def
import scala.io.Source
val fileContent = Source.fromFile(filename).getLines.toList
val content = fileContent.map(processLine)
def processLine(line: String){
val words = line.split("\\s+")
println((words(0), words(1)))
}
此处processLine不返回任何内容,因此内容现在是所有项目的空返回值列表。我认为解决办法是包括ProcessLine从一个返回值,但斯卡拉不喜欢:
warning: enclosing method processLine has result type Unit: return value discarded
所以,我怎么能修改ProcessLine从以便它可以被用来创建非空的元组的列表内容中的价值?如何用多行声明一个lambda函数?
var nonLinearTrainingContent = fileContent.map(x=> {
val words = x.split("\\s+")
(words(0), words(2))
})
andreas。你也可以用lambda函数替换processLine吗?我认为这将完成/补充主题答案。 – stian