Slide 1

Slide 1 text

I'm Konstantin and I ❤ Swift

Slide 2

Slide 2 text

I'm iOS Dev [[[[[NSObject allo] init] sendMesssagge: @"message"] getResponse: ^{ }] filtered:10];

Slide 3

Slide 3 text

[[[ ! ]]]

Slide 4

Slide 4 text

Swift var name = "Konstantin" name += " ❤️ Swift" name.add("and All the world").uppercase().trim

Slide 5

Slide 5 text

I'm iOS Dev !

Slide 6

Slide 6 text

iOS Swift Functional Programming

Slide 7

Slide 7 text

Functional Prograramming ?

Slide 8

Slide 8 text

Functional programming Functional programming is - a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data — Wiki

Slide 9

Slide 9 text

1 Simple, Truth, Functions 2 x 2 = 4

Slide 10

Slide 10 text

Simple, Truth, Functions func double(x) = x * 2 double(2) = 4 double(4) = 8 input -> (work) -> output f(x) = y

Slide 11

Slide 11 text

2 Only play with your toys

Slide 12

Slide 12 text

Only play with your toys func double(x) -> Int { x * 2 // x * MegicNumber() * Cache.getSharedCache.intFor(x) }

Slide 13

Slide 13 text

3 Divide and conquer

Slide 14

Slide 14 text

Divide and conquer Split program into smaller parts and reuse boilerplate part Functions is a Type, the same as Int

Slide 15

Slide 15 text

Divide and conquer Split program into smaller parts and reuse boilerplate part Functions is a Type, the same as Int var a: Int = 10 var b: () -> () // sayHello(), will print hello to console b = sayHello var d: Int -> Bool // isOdd(10) - false var e: (Int -> Bool) -> Int // filter(isOdd(10))

Slide 16

Slide 16 text

4 What you want ❗️How you want

Slide 17

Slide 17 text

What you want, now how you want I have an array of numbers, I want to get only odd numbers. I have an array of numbers, takes yeach element, check if it's odd, add it to the result array and return

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Model struct Cat { let name: String let quote: String let image: UIImage } Data { "name" : "Tommy", "quote" : "Tommy playing with ..." "imageUrl" : "http://lorenipsen//cats/1230.jpg" }

Slide 20

Slide 20 text

Data → Model

Slide 21

Slide 21 text

Model + Mapping struct Cat { let name: String let quote: String let image: UIImage init(json: JSON) { .... } }

Slide 22

Slide 22 text

Model + Mapping struct Cat { let name: String let quote: String let image: UIImage init(json: JSON) { .... } init(flikker: JSON) { .... } init(sweetKitti: UIImage) { .... } ... }

Slide 23

Slide 23 text

JSON -> Cat func catify(json: JSON) -> Cat { return Cat(name: json["name"], quote: json["quote"], image: UIImage(url: json["imageUrl"])) }

Slide 24

Slide 24 text

JSON -> Cat func catify(json: JSON) -> Cat { return Cat(name: json["name"], quote: json["quote"], image: UIImage(url: json["imageUrl"])) } let catData = {"name" : "Tommy", ...} let cat = catify(catData)

Slide 25

Slide 25 text

Many JSON -> Cats let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Siri", ...}, {"name" : "Lori", ...}, {"name" : "Mari", ...}] We know how to transform 1 Cat Now we want to do it for every element

Slide 26

Slide 26 text

Many JSON -> Cats let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Siri", ...}, {"name" : "Lori", ...}, {"name" : "Mari", ...}] We know how to transform 1 Cat Now we want to do it for every element let result = Aray() for elemt in catsJSON { let cat = catify(elemt) result.add(cat) }

Slide 29

Slide 29 text

More Different Cats func catify(json: JSON) -> Cat { .. } func catifyWithLogo(json: JSON) -> Cat { ... image: UIImage(url: json["imageUrl"]).add(UIImage("RFLogo")) } func superCatify(json: JSON) -> Cat { Cat(name: "Super - " json["name"], ... }

Slide 30

Slide 30 text

More Different Cats func catify(json: JSON) -> Cat { .. } func catifyWithLogo(json: JSON) -> Cat { ... image: UIImage(url: json["imageUrl"]).add(UIImage("RFLogo")) } func superCatify(json: JSON) -> Cat { Cat(name: "Super - " json["name"], ... } let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Sisi", ...}] let cats = catsJSON.map(catify) let logoCats = catsJSON.map(catifyWithLogo) let superCats = catsJSON.map(superCatify)

Slide 31

Slide 31 text

More Cats let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Sisi", ...}] let transformers = [catify, catifyWithLogo, superCatify] // type Array of (JSON -> Cat)

Slide 32

Slide 32 text

More Cats let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Sisi", ...}] let transformers = [catify, catifyWithLogo, superCatify] // type Array of (JSON -> Cat) let cats = transformers.map { transform in return catsJSON.map(transform) } // cats [[Cat], [Cat], ..]

Slide 33

Slide 33 text

Filtering let cats: [Cats] I want only cats with name Oscar // [Cats].filter(Cat -> Bool) -> [Cats]

Slide 34

Slide 34 text

Filtering let cats: [Cats] I want only cats with name Oscar // [Cats].filter(Cat -> Bool) -> [Cats] cats.filter() { $0.name == "Oscar" }

Slide 35

Slide 35 text

Filtering let cats: [Cats] I want only cats with name Oscar // [Cats].filter(Cat -> Bool) -> [Cats] cats.filter() { $0.name == "Oscar" } cats.filter() { $0.name == "Lili" } cats.filter() { $0.name == "Lora" } ^ Not DRY again

Slide 36

Slide 36 text

Return a Funtion let cats: [Cats] I want only cats with a specific name cats.filter(withName("Oscar")) cats.filter(withName("Lili")) // filter (Cat -> Bool) // withName - (String -> (Cat -> Bool))

Slide 37

Slide 37 text

Return a Funtion let cats: [Cats] func withName(catName: String) -> (Cat -> Bool) { return func catWithName(cat: Cat) -> Bool { cat.name == catName } } let oscar = withName("Oscar") // (Cat -> Bool) cats.filter(oscar) cats.filter(withName("Lili"))

Slide 38

Slide 38 text

Much More

Slide 39

Slide 39 text

Thanks Everyone! Konstantin Koval & Swift