面向对象编程与函数式编程
在编程领域,面向对象编程(OOP)和函数式编程(FP)是两种重要的编程范式。下面我们将详细探讨它们在实际代码中的应用。
1. 获取特定游戏
我们有一个 getGameByHighestScoreAndPlayedCount
方法,它有两种等效的实现方式,都用于根据最高得分和游玩次数来获取游戏:
public func getGameByHighestScoreAndPlayedCount(highestScore: Int,
playedCount: Int) -> Game? {
return getAll().filter({
(game: Game) -> Bool in
game.highestScore == highestScore && game.playedCount ==
playedCount
}).first
}
public func getGameByHighestScoreAndPlayedCount(highestScore: Int,
playedCount: Int) -> Game? {
return getAll().filter({ $0.highestScore == highestScore &&
$0.playedCount == playedCount }).first
}
2. 使用 map
转换值