Your code is not only very far from what's considered best practice in Scala in terms of variable and class naming, but also has inconsistencies:
At first, you show the code snippet:
// --- WRONG --- //
val Ana = 0
val Mario = 33
val Romero = 11
And then you show the code snippet
// --- WRONG --- //
Attendance((Mario,33),(Romero,12))
The problem is that in this situation, (Mario, 33)
just means (33, 33)
, because Mario
is a variable that you assigned to be 33
. What you should do instead is:
val ana = ("Ana", 0)
val mario = ("Mario", 33)
val romero = ("Romero", 11)
Another inconsistency is
// --- WRONG --- //
case class attendance(List(name,totalDay))
This would not compile. When writing List(name, totalDay)
you define a List
containing two elements, name
and totalDay
. These elements are not defined in your example.
What you want to do instead is:
case class Attendance(attendees: List[(String, Int)])
What does attendees: List[(String, Int)]
mean?
It means that you define a parameter to your case class Attendance
,
and this parameter is called attendees
, and is of type List[(String, Int)]
(speak: "A list of tuples, each consisting of a string and an integer")
Then you can use it:
val listOfAttendees: List[(String, Int)] = ??? // Not implemented yet
val attendance = Attendance(listOfAttendees)
But how do you make listOfAttendees
?
The answer is filter
:
val allAttendees = List(ana, mario, romero)
val filteredAttendees = allAttendees.filter {
case (name, days) => days > 0
}
Put it all together:
val ana = ("Ana", 0)
val mario = ("Mario", 33)
val romero = ("Romero", 11)
case class Attendance(attendees: List[(String, Int)])
val allAttendees = List(ana, mario, romero)
val filteredAttendees = allAttendees.filter {
case (name, days) => days > 0
}
val attendance = Attendance(filteredAttendees)
Try it out!
I hope this helps.