Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

299 views

Published on

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

Published in: Engineering
  • Be the first to comment

  • Be the first to like this

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

  1. 1. , . .
  2. 2. - https://open.kakao.com/o/ gMvUe8Kb - https://github.com/wanbok/ LetSwiftWorkShop
  3. 3. 1. 2. / 1. filter, reduce, map, flatMap 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 effect) •
  10. 10. • (Pure function) • input output, • (Anonymous function) • Swift Closure, Obj-c Block • (Higher-order function) • forEach, filter, first/last(where:), reduce • map, flatMap, 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 + flatten [[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 , flatMap function * .
  23. 23. Collection nil ["This", nil, "is", "Sparta!", nil] .compactMap { $0 } // ["This", "is", "Sparta!"]
  24. 24. flatMap • context • [String?].flatMap { element -> String? } • flatMap { 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)
  37. 37. Functor Monad
  38. 38. Container ( Context / Wrapper) • Array, Dictionary, Result, Optional Functor map Monad flatMap
  39. 39. 2. , Functor, Monad
  40. 40. • 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/

×