-
Notifications
You must be signed in to change notification settings - Fork 50
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
Tutorial1, 2 (FP, Dictionary, uniqueMap) #7
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,13 @@ import Foundation | |
... | ||
] | ||
*/ | ||
|
||
struct Champ: Decodable { | ||
let key: String | ||
let name: String | ||
} | ||
|
||
|
||
let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") | ||
|
||
/* | ||
|
@@ -24,9 +31,18 @@ let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", o | |
let champsData = FileManager.default.contents(atPath: champsFilePath!) | ||
let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFilePath!) | ||
|
||
let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) | ||
let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) | ||
let decoder = JSONDecoder() | ||
let champs = try decoder.decode([Champ].self, from: champsData!) | ||
let selectedIndexes = try decoder.decode([Int].self, from: selectedIndexesData!) | ||
|
||
// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 | ||
let names: [String] = [] | ||
|
||
let dictionary = champs.reduce(into: [String: String]()) { | ||
$0[$1.key] = $1.name | ||
} | ||
let names = selectedIndexes.map { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 익명함수를 제거하면 더 읽기 좋은 코드가 될 수 있습니다. |
||
String(describing: $0) | ||
}.compactMap { | ||
dictionary[$0] | ||
} | ||
print(names) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,40 @@ | ||
import Foundation | ||
|
||
/* | ||
[ | ||
{ | ||
"key": "266", | ||
"name": "Aatrox", | ||
... | ||
}, | ||
... | ||
] | ||
*/ | ||
struct Champ: Decodable { | ||
let key: String | ||
let name: String | ||
} | ||
|
||
|
||
let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json") | ||
|
||
/* | ||
[ | ||
1, | ||
33, | ||
... | ||
] | ||
*/ | ||
let selectedIndexesFilePath = Bundle.main.path(forResource: "selectedIndexes", ofType: "json") | ||
|
||
let champsData = FileManager.default.contents(atPath: champsFilePath!) | ||
let selectedIndexesData = FileManager.default.contents(atPath: selectedIndexesFilePath!) | ||
|
||
let champs = try JSONSerialization.jsonObject(with: champsData!, options: []) | ||
let selectedIndexes = try JSONSerialization.jsonObject(with: selectedIndexesData!, options: []) | ||
let decoder = JSONDecoder() | ||
let champs = try decoder.decode([Champ].self, from: champsData!) | ||
let selectedIndexes = try decoder.decode([Int?].self, from: selectedIndexesData!) | ||
|
||
extension Array where Element: Equatable { | ||
func uniqueMap() -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 시도 같습니다. 어떤 이유로 만드셨는지 설명을 붙여주시면, 더 좋은 리뷰를 드릴 수 있을 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 배열의 인자들에 대하여 유일한 값들로 이루어져 있으며, 그 순서를 보장한 배열을 만들고자하였습니다. |
||
return self.reduce([Element]()) { | ||
return !$0.contains($1) ? ($0 + [$1]) : $0 | ||
} | ||
} | ||
} | ||
|
||
// TODO: selectedIndexes는 챔피언 목록(champs)의 key 번호 들이다. selectedIndexes에 명시된 순서대로 챔피언들의 이름(name)을 나열하라 | ||
let names: [String] = [] | ||
let names: [String] = selectedIndexes | ||
.uniqueMap() | ||
.compactMap { $0 } | ||
.map { | ||
String(describing: $0) | ||
dev-yong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
.compactMap { key in | ||
champs.first { $0.key == key }?.name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
print(names) | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reduce를 사용해보셨군요! 좋습니다.