在進行代碼練習時,我面臨無法從 intellj 決議符號 x
錯誤代碼行 println(lst.reduceLeft((x,y) => {println(x " , " y) x y})) println(lst.reduceRight((x,y) => {println(x " , " y) x -y}))
我試圖從 intellj 的建議中除錯,但沒有作業
Intellj Build #IC-221.5591.52,于 2022 年 5 月 10 日構建
scala 版本 scala-sdk-2.11.12
參考
http://www.codebind.com/scala/scala-reduce-fold-scan-leftright/?unapproved=192475&moderation-hash=8cdabb0f7834cbe19792b863eb952538#comment-192475
//In Scala Reduce, fold or scan are most commonly used with collections in the form of reduceLeft, reduceRight, foldLeft, foldRight, scanLeft or scanRight.
// In general, all functions apply a binary operator to each element of a collection.
// The result of each step is passed on to the next step.
package pack {
}
object obj2 {
println
println("=====started=======")
println
//val is a constant (which is an un changeable variable),A variable holds a value / address to a value in memory
val lst = List(1, 2, 3, 5, 7, 10, 13)
val lst2 = List("A", "B", "C")
def main(args: Array[String]) {
println(lst.reduceLeft(_ _))
println(lst2.reduceLeft(_ _))
println(lst.reduceLeft((x,y) => {println(x " , " y) x y}))
println(lst.reduceLeft(_ - _))
println(lst.reduceRight(_ - _))
println(lst.reduceRight((x,y) => {println(x " , " y) x -y}))
println(lst.foldLeft(100)(_ _))
println(lst2.foldLeft("z")(_ _))
println(lst.scanLeft(100)(_ _))
println(lst2.scanLeft("z")(_ _))
}
}
uj5u.com熱心網友回復:
println(lst.reduceLeft((x,y) => { println(x " , " y) x y }))
里面的代碼{ }不是一個有效的運算式。看起來您希望 Scala 能夠計算出這里有兩個運算式,但它不能。你需要明確;地修復它:
println(lst.reduceLeft((x,y) => { println(x " , " y); x y }))
uj5u.com熱心網友回復:
println(lst.reduceLeft((x,y) => {println(x " , " y) x y}))無效。如果您想在匿名函式中使用多個指令,則需要將它們分開;或使其成為多行:
println(lst.reduceLeft((x, y) => { println(x " , " y); x y }))
或者
println(lst.reduceLeft((x, y) => {
println(x " , " y)
x y
}))
也更好地使用字串插值而不是連接:
println(lst.reduceLeft((x, y) => {
println(s"$x , $y")
x y
}))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496761.html
下一篇:編譯后圖示不可見
