There's a fundamental modelling issue here. Your struct person doesn't actually model a person. It models something like a RoundResult.
I would refactor this by making a Player that truly models a person, (with only fields like name: String), and make a RoundResult that contains a winner: Player and a score: Score.
struct Player: Hashable { // Perhaps should be a class, if names aren't unique.
let name: String
}
struct RoundResult {
let winner: Player
let score: Int
}
let playerA = Player(name: "a")
let playerB = Player(name: "b")
let roundResults = [
RoundResult(winner: playerA, score: 1),
RoundResult(winner: playerA, score: 3),
RoundResult(winner: playerB, score: 5),
]
let scoresByPlayer = Dictionary(grouping: roundResults, by: \.winner)
.mapValues { roundResults -> Int in
let scores = roundResults.lazy.map(\.score)
return scores.reduce(0, +)
}
print(scoresByPlayer)
From here, you can add a score variable on player, which actually models the players score, not just a single sliver of it from a single round/game/match/whatever
UpperCamelCasein Swift.let people = [("a",1), ("a",3), ("b", 5)].map(Person.init)