Last active
September 6, 2019 02:59
-
-
Save jarsen/41de7401d49cd2348e5f to your computer and use it in GitHub Desktop.
example of copy constructor with default values for immutability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Scorekeeper { | |
let runningScore: Int | |
let climbingScore: Int | |
let bikingScore: Int | |
let swimmingScore: Int | |
init(runningScore: Int = 0, climbingScore: Int = 0, bikingScore: Int = 0, swimmingScore: Int = 0) { | |
self.runningScore = runningScore | |
self.climbingScore = climbingScore | |
self.bikingScore = bikingScore | |
self.swimmingScore = swimmingScore | |
} | |
init(scoreKeeper: Scorekeeper, runningScore: Int? = nil, climbingScore: Int? = nil, bikingScore: Int? = nil, swimmingScore: Int? = nil) { | |
self.runningScore = runningScore ?? scoreKeeper.runningScore | |
self.climbingScore = climbingScore ?? scoreKeeper.climbingScore | |
self.bikingScore = bikingScore ?? scoreKeeper.bikingScore | |
self.swimmingScore = swimmingScore ?? scoreKeeper.swimmingScore | |
} | |
func incrementRunningScoreBy(points: Int) -> Scorekeeper { | |
return Scorekeeper(scoreKeeper: self, runningScore: self.runningScore + points) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great idea. I have question, what would you do, says you have an array of Scorekeepers and you want to update element where
runningScore
equal to10
and also the position of element after updated has to be the same as it were. I don't know how to approach this in Functional Programming way. Could you enlighten me?