We can chain filter
, map
, and reduce
. The following lines declare a new
calculateGamesHighestScoresSum
method for our previously coded GameRepository
class that chains filter
, map
, and reduce
calls:
public func calculateGamesHighestScoresSum(minPlayedCount: Int) -> Int { return getAll().filter({ $0.playedCount >= minPlayedCount }).map({ $0.highestScore }).reduce(0) { sum, highestScore in return sum + highestScore } }
The calculateGamesHighestScoresSum
method receives a minPlayedCount
argument of the Int
type and returns an Int
value. The code calls the getAll
and filter
methods to generate a new Array<Game>
with only the Game
instances, whose playedCount
value is greater than or equal to the value specified in the minPlayedCount
argument. The code calls the map
method to transform an Array<Game>
into an Array<Int>
with the values specified in the highestScore
stored property. Then, the code calls the reduce
method...