,

.

.
- https://open.kakao.com/o/
gMvUe8Kb



- https://github.com/wanbok/
LetSwiftWorkShop
1. 

2. / 

1. filter, reduce, map, flatMap 

2. iOS Swift 

3. Git, Github 

3.
(Cognitive Complexity) 

SonarQube
.

1. .

2. (
) 1 .

3. .
4 .

A. (Nesting) - 

B. (Structural) - + 

C. Fundamental -


D. Hybrid -
(Cyclomatic Complexity) 



• Callback hell

• If else 

SonarQube ,
0. Tutorial
• 

• 

• 

• 

• (Side effect) 

•
• (Pure function)

• input output, 

• (Anonymous function)

• Swift Closure, Obj-c Block

• (Higher-order function)

• forEach, filter, first/last(where:), reduce

• map, flatMap, compactMap
For-in loop 

• loop
["This", "is", “Sparta!"]
.forEach { print($0) }
// This
// is
// Sparta!
(Element)
["This", "is", “Sparta!"]
.filter { $0.contains("is") }
.forEach { print($0) }
// This
// is
["This", "is", "Sparta!"]
.first(where: { $0.contains("is") })?
.forEach { print($0) }
// This
["This", "is", "Sparta!"]
.reduce("", { (sentence, word) -> String in
sentence + " " + word
})
// "This is Sparta!"
, Type
(Transform)
["This", "is", "Sparta!"]
.map { (text) -> Int in return text.count }
// .map { $0.count }
// [4, 2, 7]
Dictionary (key, value) tuple
.

.

let result: [String] = dict
.map { (each: (key: String, value: Int)) -> String in
"(each.key): (each.value)"
}
map 

let priceString = "100 "
let parsedNumber: Int? = Int(priceString)
parsedNumber.map { “($0 / 10) ” }
// Optional(“10 ")
Context
(Container)
Collection, Optional, Result , value
Value
(Content)
, context
Transform
(Function)
value value
(Value) Context , map function
* .
context 

• map + flatten
[[1, 2, 3], [4, 5, 6], []]
.flatMap { (numberList) -> [Int] in
return numberList.map { $0 * $0 }
}
// [1, 4, 9, 16, 25, 36]
context 

• (Transfrom)
let number: Int? = 10
number
.flatMap { (number) -> String? in
guard number % 2 == 0 else { return nil }
return “(number) ”
}
// Optional.some Optional.none
(Value) Context , flatMap
function
* .
Collection nil
["This", nil, "is", "Sparta!", nil]
.compactMap { $0 }
// ["This", "is", "Sparta!"]
flatMap 

• context 

• [String?].flatMap { element -> String? }

• flatMap { element -> [String?] }
1.
,
• .

• .

• .

• .

•
• .

• .

• .

• .

• (Nested) 

• Non-local
class Model {
init(dictionary: [String: Any]) {
…
}
}
let dictionary: [String: Any]? = …
if let dictionary = dictionary {
let model = Model(dictionary: dictionary)
}
class Model {
init(dictionary: [String: Any]) {
…
}
}
let dictionary: [String: Any]? = …
dictionary.map { Model(dictionary: $0) }
class Model {
init(dictionary: [String: Any]) {
…
}
}
let dictionary: [String: Any]? = …
dictionary.map({ Model(dictionary: $0) })
class Model {
init(dictionary: [String: Any]) {
…
}
}
let dictionary: [String: Any]? = …
dictionary.map(Model.init)
Observable.just(())
.observeOn(MainScheduler.instance)
.flatMap(rx.closeAnimation)
.bind(to: rx.removeFromSuperview)
.disposed(by: disposeBag)
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)
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)
}
}
let dependency: ViewController.Dependency = .init(
pushDetailView: { (viewController) in
viewController
.navigationController?
.pushViewController(viewController, animated: true)
},
presentLoginView: { (viewController) in … }
)
let viewController = ViewController(dependency: dependency)
Functor Monad
Container ( Context / Wrapper)

• Array, Dictionary, Result, Optional 

Functor map

Monad flatMap
2. , Functor, Monad
• 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/
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵

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