我是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