patternswiftMinor
Registering Pokémon weaknesses
Viewed 0 times
registeringweaknessespokémon
Problem
I recently answered a question that had me wondering if I could simplify this below code without creating an extra function:
Essentially the code loops through objects (list of pokemon) and is designed to add all of the pokemon weak to type (and optionally typeTwo).
I ended up just refactoring the
I know I could simplify it a bit by using a map instead of
Any ideas?
for object in objects {
if let type = object["type"] where !type.isEmpty, let name = object["name"] {
print(pokemonTypeDefenseChart[type])
for weakness in pokemonTypeDefenseChart[type]! {
if pokemonWeaknessChart[weakness] == nil {
pokemonWeaknessChart[weakness] = []
}
pokemonWeaknessChart[weakness]?.append(name)
}
}
if let typeTwo = object["typeTwo"] where !typeTwo.isEmpty, let name = object["name"] {
for weakness in pokemonTypeDefenseChart[typeTwo]! {
if pokemonWeaknessChart[weakness] == nil {
pokemonWeaknessChart[weakness] = []
}
pokemonWeaknessChart[weakness]?.append(name)
}
}
}Essentially the code loops through objects (list of pokemon) and is designed to add all of the pokemon weak to type (and optionally typeTwo).
I ended up just refactoring the
for statements inside the if let statements into a function, but I was wondering if it was possible to compress these without an extra function. The only difference is the type.I know I could simplify it a bit by using a map instead of
for loops but I wanted to keep it more understandable for the question.Any ideas?
Solution
You could do something like:
Also, to simplify the rest of your code, you could extend
This allows you to replace this:
by this:
for x in ["type", "typeTwo"] {
if let type = object[x] where !type.isEmpty,
let name = object["name"],
defenseChart = pokemonTypeDefenseChart[type] { /* ... */ }
}Also, to simplify the rest of your code, you could extend
Dictionary like this:extension Dictionary {
subscript(key: Key, fallback fallback: Value) -> Value {
get { return self[key] ?? fallback }
set { self[key] = newValue }
}
}This allows you to replace this:
for weakness in defenseChart {
if pokemonWeaknessChart[weakness] == nil {
pokemonWeaknessChart[weakness] = []
}
pokemonWeaknessChart[weakness]?.append(name)
}by this:
for weakness in defenseChart {
pokemonWeaknessChart[weakness, fallback: []].append(name)
}Code Snippets
for x in ["type", "typeTwo"] {
if let type = object[x] where !type.isEmpty,
let name = object["name"],
defenseChart = pokemonTypeDefenseChart[type] { /* ... */ }
}extension Dictionary {
subscript(key: Key, fallback fallback: Value) -> Value {
get { return self[key] ?? fallback }
set { self[key] = newValue }
}
}for weakness in defenseChart {
if pokemonWeaknessChart[weakness] == nil {
pokemonWeaknessChart[weakness] = []
}
pokemonWeaknessChart[weakness]?.append(name)
}for weakness in defenseChart {
pokemonWeaknessChart[weakness, fallback: []].append(name)
}Context
StackExchange Code Review Q#138312, answer score: 2
Revisions (0)
No revisions yet.