Slide 1

Slide 1 text

Matryoshka 
 in Swift Krzysztof Siejkowski, @_siejkowski

Slide 2

Slide 2 text

No content

Slide 4

Slide 4 text

Slide 5

Slide 5 text

Result A E

Slide 6

Slide 6 text

Either A B Result A E

Slide 7

Slide 7 text

Either A B Result A E Future ((A) -> Void) -> Void

Slide 8

Slide 8 text

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Slide 12

Slide 12 text

forEach A (A) -> Void

Slide 13

Slide 13 text

forEach A (A) -> Void A A reduce

Slide 14

Slide 14 text

forEach A (A) -> Void A (A) -> Container B flatMap A A reduce

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

As a user, I want to see all my Github repos written in Swift language

Slide 17

Slide 17 text

1. Fetch data from Github 2. Deserialize 3. Filter Swift repos

Slide 18

Slide 18 text

githubClient .fetch(.repos, for: user) .map { deserialize($0) } .filter { $0.isSwiftRepo }

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

1. Fetch data from Github extension URLSession { func dataTask( with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void ) -> URLSessionDataTask }

Slide 21

Slide 21 text

1. Fetch data from Github completionHandler => Future (Data?, URLResponse?, Error?) => Result Future>

Slide 22

Slide 22 text

2. Deserialize Result is a JSON object in form of an array of repos: [ { “name”: “repo_name”, ... }, ... We want to know which ones of the repos couldn’t be deserialized deserialize: (Data) throws -> [Data] throws -> [Repo]

Slide 23

Slide 23 text

2. Deserialize deserialize: (Data) throws -> [Data] throws -> [Repo] => (Data) -> Result<[Data], DeserializationError> -> Result<[Result], DeserializationError> Future], DeserializationError >, NetworkError >>

Slide 24

Slide 24 text

3. Filter Swift repos filter: (Repo) -> Bool repos.filter { (repo: Repo) -> Bool in repo.language == "Swift" } "Swift"

Slide 25

Slide 25 text

3. Filter Swift repos let repos: Future], DeserializationError >, NetworkError >> = githubClient.fetch(.repos, for: user) .map { deserialize($0) } repos.filter { }

Slide 26

Slide 26 text

Future< Result< Result< Array< Result< Repo, DeserializationError > >, DeserializationError >, NetworkError > > ❌

Slide 27

Slide 27 text

Simplifying by transforming

Slide 28

Slide 28 text

What’s the difference? Optional A ∅ Either A Void

Slide 29

Slide 29 text

You can write a transform! Optional A ∅ Either A Void Optional A ∅ Either A Void

Slide 30

Slide 30 text

Validated A E E E E E E Result A E

Slide 31

Slide 31 text

extension Result { func asValidated() -> Validated { ... } } Future< Validated< Validated< Array>, DeserializationError >, NetworkError > >

Slide 32

Slide 32 text

sequence A A Container1> => Container2>

Slide 33

Slide 33 text

Given: 
 Array> Sequence: If any element of array is nil, then return nil Else, return array of unwrapped, non-nil elements Then: Optional>

Slide 34

Slide 34 text

func sequence(_ vs: [Validated]) -> Validated<[A], E> Future< Validated< Validated< Validated< Array, DeserializationError >, DeserializationError >, NetworkError > >

Slide 35

Slide 35 text

Container> => Container flatten A A

Slide 36

Slide 36 text

extension Validated { func flatten() -> Validated where A == Validated { ... } } Future< Validated< Validated< Array, DeserializationError >, NetworkError > >

Slide 37

Slide 37 text

extension Validated { func mapErrors(_ f: (E) -> F) -> Validated { ... } } // put errors into Either container Future< Validated< Validated< Array, Either >, Either > >

Slide 38

Slide 38 text

We’re left with 3 containers-deep nesting // after flattening again Future< // 1st level Validated< // 2nd level [Repo], // 3rd level Either > >

Slide 39

Slide 39 text

Filter Swift repos let repos: Future< Validated< Array, Either > > = githubClient.fetch(.repos, for: user) .map { deserialize($0) } .map { // transforms described before } repos.filter { }

Slide 40

Slide 40 text

Write “nested” methods for containers

Slide 41

Slide 41 text

Container1> + (A) -> B = Container1> innerMap B A (A) -> B

Slide 42

Slide 42 text

Two ways of implementing: • protocol-based • method-based

Slide 43

Slide 43 text

Inverse of type erasure protocol Protocol { associatedtype A } Type erasure struct AnyProtocol 
 : Protocol struct Container “Type rasure” protocol ContainerType { associatedtype A }

Slide 44

Slide 44 text

1. Create protocol protocol ValidatedType { associatedtype PA associatedtype PE } extension Validated: ValidatedType { typealias PA = A typealias PE = E }

Slide 45

Slide 45 text

2. Duplicate API in protocol (return concrete type) protocol ValidatedType { associatedtype PA associatedtype PE func map(_ f: (PA) -> B) -> Validated } extension Validated: ValidatedType {}

Slide 46

Slide 46 text

3. Implement the nested method in a constrained extension extension Future where A: ValidatedType { func map( _ f: @escaping (A.PA) -> B ) -> Future> { return map { $0.map(f) } } }

Slide 47

Slide 47 text

Method-based 1. Generalize the method,
 not extension 2. Add constraints to 
 the method, not extension

Slide 48

Slide 48 text

1. Generalize the method, not extension extension Future { func map( _ f: @escaping (VA) -> B ) -> Future> { // work in progress } }

Slide 49

Slide 49 text

2. Add constraints to the method, not extension extension Future { func map( _ f: @escaping (VA) -> B ) -> Future> where A == Validated { return map { $0.map(f) } } }

Slide 50

Slide 50 text

Now we can filter Swift repos! let repos: Future< Validated< Array, Either >> = githubClient.fetch(.repos, for: user) .map { deserialize($0) } .map { // transforms described before } repos.filter { (repo: Repo) -> Bool in repo.language == “Swift” } // it works!

Slide 51

Slide 51 text

A lot of 
 boilerplate?

Slide 52

Slide 52 text

Use code generation (like Sourcery) Please see this great talk for details:
 Elviro Rocca — Protocol-Oriented Monad Transformers https://www.youtube.com/watch?v=Zmb86zblcto

Slide 53

Slide 53 text

Use “wide” container, like
 Observable

Slide 54

Slide 54 text

Future< <== observable is async Validated< <== observable can return error Array< <== observable is sequence Repo >, Either > >

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Thanks! Questions?