I am new to Scala and I am trying to execute = following code in Scala:
scala> case class FoldExp(firstname : String, lname : String, age : Int, sex : String)
defined class FoldExp
scala> object Foo{
| def apply(firstname : String, lname : String, age : Int, sex : String) = new FoldExp(firstname,lname,age,sex)
| }
defined object 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"
}
The above block of code threw following error:
<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"
I want to categorize people in the list into their respective bands based on their age. But iam not able to achieve this using pattern matching. Could anyone tell me why the above approach is wrong and what is the method to be followed to achieve my objective. Any attempt to find solution to the above problem is appreciated. Thanks in advance.