Practical Declarative Programming (360 iDev 2015)

Practical Declarative Programming (360 iDev 2015)

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

August 19, 2015
Tweet

Transcript

  1. 20.
  2. 22.

    Traditionally let fetchRequest = NSFetchRequest(entityName: "Person") fetchRequest.predicate = NSPredicate(format: "name

    == %@", "Kyle") fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)] var error:NSErrorPointer? var objects = managedObjectContext.executeFetchRequest(fetchRequest, error: error) if let objects = objects { ... }
  3. 26.

    What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  4. 27.

    What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  5. 31.

    Un-functional Function func increment() { let center = NSNotificationCenter.defaultCenter() let

    value = center.valueForKey("key") as? Int ?? 0 center.setValue(value + 1, forKey: "key") }
  6. 35.

    High Order Functions 4 Functions that take other functions as

    arguments 4 Functions that returns a functions
  7. 40.

    void message(NSString *value) { } In Objective-C, we can also

    declare functions. This may resemble the declaration method slightly from our previous examples.
  8. 43.
  9. 47.

    let message:(String -> ()) We can define a constant that

    is callable using this syntax. It's very similar to above, we have taken the definition from the previous slide and prefixed the variable name.
  10. 55.

    func message1(value:String) { println(“Hello \(value)”) } var message2:(String -> Void)

    = { value in println(“Hi \(value)”) } message2 = message1 message2("360 iDev") // Hello 360 iDev
  11. 59.

    How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] var counts = [Int]() for group in groups { let people = group.count counts.append(people) } counts // [2, 3]
  12. 60.

    map

  13. 63.

    How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map { $0.count } // [2, 3]
  14. 64.

    How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map { count($0) } // [2, 3]
  15. 68.

    How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map(count) // [2, 3]
  16. 71.
  17. 75.

    Order the numbers of people in each group let count

    = [2, 3] count.sort { (lhs, rhs) in lhs > rhs } // largest group is [3, 2].first
  18. 80.

    Building an array of all people let groups = [["Kyle",

    "Katie"], ["André", "Maxine", "Ash"]] var people = [String]() for group in groups { people += group } // ["Kyle", "Katie", "André", "Maxine", "Ash"]
  19. 81.
  20. 86.

    Building an array of all people let groups = [["Kyle",

    "Katie"], ["André", "Maxine", "Ash"]] groups.reduce([], combine: +) // ["Kyle", "Katie", "André", "Maxine", "Ash"]
  21. 88.

    let input = "Kyle,Katie\nAndré,Maxine,Ash" var groups = [[String]]() for line

    in input.componentsSeparatedByString("\n") { let group = line.componentsSeparatedByString(",") groups.append(group) } groups // [["Kyle", "Katie"], ["André", "Maxine", "Ash"]]
  22. 89.

    let input = "Kyle,Katie\nAndré,Maxine,Ash" let groups = input.componentsSeparatedByString("\n").map { line

    in line.componentsSeparatedByString(",") } groups // [["Kyle", "Katie"], ["André", "Maxine", "Ash"]]
  23. 90.

    let input = "Kyle,Katie\nAndré,Maxine,Ash" func commaSeparator(input:String) -> [String] { return

    input.componentsSeparatedByString(",") } input.componentsSeparatedByString("\n").map(commaSeparator) // [["Kyle", "Katie"], ["André", "Maxine", "Ash"]]
  24. 92.

    func separateBy(separator:String) -> ((String) -> [String]) { func inner(source:String) ->

    [String] { return source.componentsSeparatedByString(separator) } return inner }
  25. 93.

    func separateBy(separator:String) -> ((String) -> [String]) { func inner(source:String) ->

    [String] { return source.componentsSeparatedByString(separator) } return inner } let lineSeparator = separateBy("\n") let commaSeparator = separateBy(",")
  26. 100.
  27. 105.

    Conclusion 4 DSLs can be used to reduce bugs and

    build simpler generic declarative languages 4 We’ve seen how Functional Programming can be used to build declarative code 4 We’ve seen how declarative code helps simplicity, test-ability and on boarding new developers