Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Dictionary default values #97

Open
Samasaur1 opened this issue Jun 1, 2022 · 0 comments
Open

Use Dictionary default values #97

Samasaur1 opened this issue Jun 1, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers internal change Something that only changes on the inside low priority Something that is not urgent

Comments

@Samasaur1
Copy link
Owner

I was reading an article about changes introduced in Swift 4 and came across the ability to provide a default value when accessing a dictionary:

let dict = getDict()

let val = dict[“key”] ?? 0
//can be replaced with
let val = dict[“key”, default: 0]

//no big deal, right? Look at this:
dict[“key”] = (dict[“key”] ?? 0) + 1
//becoming
dict[“key”, default: 0] += 1

We could take advantage of this in quite a few places in Dice.Swift, and possibly other places:

newDice[d] = (newDice[d] ?? 0) + 1

newDice[d] = (newDice[d] ?? 0) + 1

newDice[die] = (newDice[die] ?? 0) + count

newDice[d] = (newDice[d] ?? 0) + 1

newDice[d] = (newDice[d] ?? 0) + 1

newDice[die] = (newDice[die] ?? 0) + count

dice[sides] = (dice[sides] ?? 0) - 1

dice[sides] = (dice[sides] ?? 0) - multiplier

dice[sides] = (dice[sides] ?? 0) + 1

dice[sides] = (dice[sides] ?? 0) + multiplier

dice[sides] = (dice[sides] ?? 0) - 1

dice[sides] = (dice[sides] ?? 0) - multiplier

newProbs[res + nRes] = (newProbs[res + nRes] ?? .zero) + (chance * nChance)

@Samasaur1 Samasaur1 added enhancement New feature or request good first issue Good for newcomers low priority Something that is not urgent internal change Something that only changes on the inside labels Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers internal change Something that only changes on the inside low priority Something that is not urgent
Projects
None yet
Development

No branches or pull requests

1 participant