Successfully reported this slideshow.

×

# [Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵

Let'Swift 2019 첫번째 워크샵
실용적인 함수형 프로그래밍

Let'Swift 2019 첫번째 워크샵
실용적인 함수형 프로그래밍

## More Related Content

### [Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵

1. 1. , . .
2. 2. - https://open.kakao.com/o/ gMvUe8Kb - https://github.com/wanbok/ LetSwiftWorkShop
3. 3. 1. 2. / 1. ﬁlter, reduce, map, ﬂatMap 2. iOS Swift 3. Git, Github 3.
4. 4. (Cognitive Complexity) SonarQube
5. 5. . 1. . 2. ( ) 1 . 3. .
6. 6. 4 . A. (Nesting) - B. (Structural) - + C. Fundamental - D. Hybrid -
7. 7. (Cyclomatic Complexity) • Callback hell • If else SonarQube ,
8. 8. 0. Tutorial
9. 9. • • • • • (Side eﬀect) •
10. 10. • (Pure function) • input output, • (Anonymous function) • Swift Closure, Obj-c Block • (Higher-order function) • forEach, ﬁlter, ﬁrst/last(where:), reduce • map, ﬂatMap, compactMap
11. 11. For-in loop • loop ["This", "is", “Sparta!"] .forEach { print(\$0) } // This // is // Sparta!
12. 12. (Element) ["This", "is", “Sparta!"] .filter { \$0.contains("is") } .forEach { print(\$0) } // This // is
13. 13. ["This", "is", "Sparta!"] .first(where: { \$0.contains("is") })? .forEach { print(\$0) } // This
14. 14. ["This", "is", "Sparta!"] .reduce("", { (sentence, word) -> String in sentence + " " + word }) // "This is Sparta!"
15. 15. , Type (Transform) ["This", "is", "Sparta!"] .map { (text) -> Int in return text.count } // .map { \$0.count } // [4, 2, 7]
16. 16. Dictionary (key, value) tuple . . let result: [String] = dict .map { (each: (key: String, value: Int)) -> String in "(each.key): (each.value)" }
17. 17. map let priceString = "100 " let parsedNumber: Int? = Int(priceString) parsedNumber.map { “(\$0 / 10) ” } // Optional(“10 ")
18. 18. Context (Container) Collection, Optional, Result , value Value (Content) , context Transform (Function) value value
19. 19. (Value) Context , map function * .
20. 20. context • map + ﬂatten [[1, 2, 3], [4, 5, 6], []] .flatMap { (numberList) -> [Int] in return numberList.map { \$0 * \$0 } } // [1, 4, 9, 16, 25, 36]
21. 21. context • (Transfrom) let number: Int? = 10 number .flatMap { (number) -> String? in guard number % 2 == 0 else { return nil } return “(number) ” } // Optional.some Optional.none
22. 22. (Value) Context , ﬂatMap function * .
23. 23. Collection nil ["This", nil, "is", "Sparta!", nil] .compactMap { \$0 } // ["This", "is", "Sparta!"]
24. 24. ﬂatMap • context • [String?].ﬂatMap { element -> String? } • ﬂatMap { element -> [String?] }
25. 25. 1.
26. 26. ,
27. 27. • . • . • . • . •
28. 28. • . • . • . • . • (Nested) • Non-local
29. 29. class Model { init(dictionary: [String: Any]) { … } } let dictionary: [String: Any]? = … if let dictionary = dictionary { let model = Model(dictionary: dictionary) }
30. 30. class Model { init(dictionary: [String: Any]) { … } } let dictionary: [String: Any]? = … dictionary.map { Model(dictionary: \$0) }
31. 31. class Model { init(dictionary: [String: Any]) { … } } let dictionary: [String: Any]? = … dictionary.map({ Model(dictionary: \$0) })
32. 32. class Model { init(dictionary: [String: Any]) { … } } let dictionary: [String: Any]? = … dictionary.map(Model.init)
33. 33. Observable.just(()) .observeOn(MainScheduler.instance) .flatMap(rx.closeAnimation) .bind(to: rx.removeFromSuperview) .disposed(by: disposeBag)
34. 34. 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)
35. 35. 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) } }
36. 36. let dependency: ViewController.Dependency = .init( pushDetailView: { (viewController) in viewController .navigationController? .pushViewController(viewController, animated: true) }, presentLoginView: { (viewController) in … } ) let viewController = ViewController(dependency: dependency)