Functional Swift

Konstantin
November 06, 2014

Functional Swift

Introduction to function programming in swift

Konstantin

November 06, 2014

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

39 slides
  1. Swift var name = "Konstantin" name += " ❤️ Swift"

    name.add("and All the world").uppercase().trim
  2. 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
  3. Simple, Truth, Functions func double(x) = x * 2 double(2)

    = 4 double(4) = 8 input -> (work) -> output f(x) = y
  4. Only play with your toys func double(x) -> Int {

    x * 2 // x * MegicNumber() * Cache.getSharedCache.intFor(x) }
  5. Divide and conquer Split program into smaller parts and reuse

    boilerplate part Functions is a Type, the same as Int
  6. 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))
  7. 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
  8. 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" }
  9. Model + Mapping struct Cat { let name: String let

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

    quote: String let image: UIImage init(json: JSON) { .... } init(flikker: JSON) { .... } init(sweetKitti: UIImage) { .... } ... }
  11. JSON -> Cat func catify(json: JSON) -> Cat { return

    Cat(name: json["name"], quote: json["quote"], image: UIImage(url: json["imageUrl"])) }
  12. 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)
  13. 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
  14. 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) }
  15. Map function map<A>(func: A -> B ) -> [B] func

    dobule(x: Int) -> String { return String(x * 2) } [1,2,3].map(double) // ["2", "4", "6"]
  16. Map function map<A>(func: A -> B ) -> [B] func

    catify(json: JSON) -> Cat {...} // JSON -> Cat let catsJSON = [{"name" : "Tommy", ...}, {"name" : "Sisi", ...}] let cats = catsJSON.map(catify)
  17. 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"], ... }
  18. 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)
  19. More Cats let catsJSON = [{"name" : "Tommy", ...}, {"name"

    : "Sisi", ...}] let transformers = [catify, catifyWithLogo, superCatify] // type Array of (JSON -> Cat)
  20. 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], ..]
  21. Filtering let cats: [Cats] I want only cats with name

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

    Oscar // [Cats].filter(Cat -> Bool) -> [Cats] cats.filter() { $0.name == "Oscar" }
  23. 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
  24. 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))
  25. 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"))

More Decks by Konstantin

Other Decks in Programming