我是 Scala 的新手,我正在尝试在 Scala 中执行 = 以下代码:
scala> case class FoldExp(firstname : String, lname : String, age : Int, sex : String)
定义类 FoldExp
scala> object Foo{
| def apply(firstname : String, lname : String, age : Int, sex : String) = new FoldExp(firstname,lname,age,sex)
| }
定义对象 Foo
scala> val foldList = List(Foo("Hugh", "Jass", 25, "male"),Foo("Biggus"," Dickus", 43, "male"),Foo("Incontinentia", "Buttocks", 37, "female"))
foldList: List[FoldExp] = List(FoldExp(Hugh,Jass,25,male), FoldExp(Biggus, Dickus,43,male), FoldExp(Incontinentia,Buttocks,37,female))
val secTry = foldList.foldLeft(List[String]()){(w,f) =>
val comment = f.age match{
case (f.age == 25) => "Band A"
case (f.age > 30) => "Band B"
case (f.age > 50) => "Band D"
}
w:+ s"${f.firstname},${f.age} $comment"
}
上面的代码块引发了以下错误:
<console>:11: error: not found: value foldList
val secTry = foldList.foldLeft(List[String]()){(w,f) =>
^
<console>:13: error: not found: value ==
case (f.age == 25) => "Band A"
^
<console>:14: error: not found: value >
case (f.age > 30) => "Band B"
^
<console>:15: error: not found: value >
case (f.age > 50) => "Band D"
我想根据年龄将列表中的人分类到他们各自的乐队中。但是我无法使用模式匹配来实现这一点。谁能告诉我为什么上述方法是错误的,以及实现我的目标要遵循的方法是什么。任何为上述问题寻找解决方案的尝试都是值得赞赏的。提前致谢。