Contenu connexe Similaire à [Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵 (20) [Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵5. 1.
2. /
1. filter, reduce, map, flatMap
2. iOS Swift
3. Git, Github
3.
17. •
•
•
•
• (Side effect)
•
19. • (Pure function)
• input output,
• (Anonymous function)
• Swift Closure, Obj-c Block
• (Higher-order function)
• forEach, filter, first/last(where:), reduce
• map, flatMap, compactMap
21. For-in loop
• loop
["This", "is", “Sparta!"]
.forEach { print($0) }
// This
// is
// Sparta!
26. Dictionary (key, value) tuple
.
.
let result: [String] = dict
.map { (each: (key: String, value: Int)) -> String in
"(each.key): (each.value)"
}
27. map
let priceString = "100 "
let parsedNumber: Int? = Int(priceString)
parsedNumber.map { “($0 / 10) ” }
// Optional(“10 ")
30. context
• map + flatten
[[1, 2, 3], [4, 5, 6], []]
.flatMap { (numberList) -> [Int] in
return numberList.map { $0 * $0 }
}
// [1, 4, 9, 16, 25, 36]
31. context
• (Transfrom)
let number: Int? = 10
number
.flatMap { (number) -> String? in
guard number % 2 == 0 else { return nil }
return “(number) ”
}
// Optional.some Optional.none
34. flatMap
• context
• [String?].flatMap { element -> String? }
• flatMap { element -> [String?] }
39. class Model {
init(dictionary: [String: Any]) {
…
}
}
let dictionary: [String: Any]? = …
if let dictionary = dictionary {
let model = Model(dictionary: dictionary)
}
44. var contraints: ((ConstraintMaker) -> Void) {
switch self {
case .top:
return {
$0.width.equalTo(size.width)
$0.height.equalTo(size.height).priority(.high)
$0.height.lessThanOrEqualTo(self.maxHeight)
$0.top.left.right.equalToSuperview()
}
case .middle:
return {
$0.height.equalTo(size.height).priority(.high)
$0.height.lessThanOrEqualTo(maxHeight)
$0.top.equalToSuperview().inset(topOffset)
$0.left.right.equalToSuperview().inset(margins)
}
}
}
…
view.snp.makeConstraints(type.contraints)
45. class ViewController: UIViewController {
struct Dependency {
let pushDetailView: (ViewController) -> Void
let presentLoginView: (ViewController) -> Void
}
let dependency: Dependency
init(dependency: Dependency) {
self.dependency = dependency
}
func tapDetailViewButton() {
self.dependency.pushDetailView(self)
}
}
46. let dependency: ViewController.Dependency = .init(
pushDetailView: { (viewController) in
viewController
.navigationController?
.pushViewController(viewController, animated: true)
},
presentLoginView: { (viewController) in … }
)
let viewController = ViewController(dependency: dependency)
48. Container ( Context / Wrapper)
• Array, Dictionary, Result, Optional
Functor map
Monad flatMap
50. • Cognitive Complexity
• https://www.sonarsource.com/resources/white-papers/cognitive-complexity.html
•
• https://ko.wikipedia.org/wiki/
%ED%95%A8%EC%88%98%ED%98%95_%ED%94%84%EB%A1%9C%EA%B7%B8
%EB%9E%98%EB%B0%8D
• Swift Functional Programming by (Naver)
•
• https://ko.wikipedia.org/wiki/%EC%9D%BC%EA%B8%89_%EA%B0%9D%EC%B2%B4
• Functors are containers
• https://bartoszmilewski.com/2014/01/14/functors-are-containers/