Slide 1

Slide 1 text

DIY your own Combine framework Basic Functional Reactive Programming concepts Li-Heng Hsu 許立衡 @ iPlayground 2019

Slide 2

Slide 2 text

– Apple documentation "The Combine framework provides a declarative Swift API for processing values over time."

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Concepts • Builder pattern • map(_:) method

Slide 5

Slide 5 text

Builder pattern • For constructing instances • Like initializer, factory method… 1. Create a builder struct 2. Store construction info in it 3. Call its build() method to construct a product

Slide 6

Slide 6 text

URLComponents URL builder

Slide 7

Slide 7 text

Combine Subscription builder

Slide 8

Slide 8 text

Builder pattern Combine Builder Publisher Construction info Operator build() Subscriber Product Subscription Roles

Slide 9

Slide 9 text

CombineDIY.swift

Slide 10

Slide 10 text

map(_:) method • There are different worlds in code • World of Uncertainty, World of Plurality, World of Erroneous... • Instances in these worlds can't be manipulated normally

Slide 11

Slide 11 text

Optional World of Uncertainty

Slide 12

Slide 12 text

World of Uncertainty Cat? (Cat) -> Double (Cat?) -> Double? map(_:)

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Imperative Functional Metaphor Unwrap values Stack operations Optional if let x = x { /* ... */ } x.map { x in /* ... */ } Collection for i in x { /* ... */ } x.forEach { i in /* ... */ } Result if case .success(let x) = result { /* ... */ } result.map { x in /* ... */ }

Slide 15

Slide 15 text

World of Plurality [Cat] (Cat) -> Double ([Cat]) -> [Double] map(_:)

Slide 16

Slide 16 text

World of Temporality Publisher (Cat) -> Double (Publisher) ->
 Publisher map(_:)

Slide 17

Slide 17 text

DIY map(_:)

Slide 18

Slide 18 text

Feedback Come find me! Twitter: @lihenghsu