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

Tutorial1, 2 (FP, Dictionary, uniqueMap) #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Tutorial/Tutorial.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import Foundation
...
]
*/

struct Champ: Decodable {
let key: String
let name: String
}


let champsFilePath = Bundle.main.path(forResource: "champs", ofType: "json")

/*
Expand All @@ -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]()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce를 사용해보셨군요! 좋습니다.

$0[$1.key] = $1.name
}
let names = selectedIndexes.map {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

익명함수를 제거하면 더 읽기 좋은 코드가 될 수 있습니다.

String(describing: $0)
}.compactMap {
dictionary[$0]
}
print(names)
50 changes: 29 additions & 21 deletions Tutorial/Tutorial2.playground/Contents.swift
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 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 시도 같습니다. 어떤 이유로 만드셨는지 설명을 붙여주시면, 더 좋은 리뷰를 드릴 수 있을 것 같아요.

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uniqueMap 같이 이곳도 적절한 이름을 가진 함수를 만들 수 있을 것 같아요

}
print(names)