Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Practical Declarative Programming (360 iDev 2015)

Practical Declarative Programming (360 iDev 2015)

Kyle Fuller

August 19, 2015
Tweet

More Decks by Kyle Fuller

Other Decks in Technology

Transcript

  1. 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 { ... }
  2. What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  3. What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  4. Un-functional Function func increment() { let center = NSNotificationCenter.defaultCenter() let

    value = center.valueForKey("key") as? Int ?? 0 center.setValue(value + 1, forKey: "key") }
  5. High Order Functions 4 Functions that take other functions as

    arguments 4 Functions that returns a functions
  6. void message(NSString *value) { } In Objective-C, we can also

    declare functions. This may resemble the declaration method slightly from our previous examples.
  7. 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.
  8. func message1(value:String) { println(“Hello \(value)”) } var message2:(String -> Void)

    = { value in println(“Hi \(value)”) } message2 = message1 message2("360 iDev") // Hello 360 iDev
  9. 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]
  10. map

  11. How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map { $0.count } // [2, 3]
  12. How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map { count($0) } // [2, 3]
  13. How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] groups.map(count) // [2, 3]
  14. 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
  15. 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"]
  16. Building an array of all people let groups = [["Kyle",

    "Katie"], ["André", "Maxine", "Ash"]] groups.reduce([], combine: +) // ["Kyle", "Katie", "André", "Maxine", "Ash"]
  17. 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"]]
  18. let input = "Kyle,Katie\nAndré,Maxine,Ash" let groups = input.componentsSeparatedByString("\n").map { line

    in line.componentsSeparatedByString(",") } groups // [["Kyle", "Katie"], ["André", "Maxine", "Ash"]]
  19. 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"]]
  20. func separateBy(separator:String) -> ((String) -> [String]) { func inner(source:String) ->

    [String] { return source.componentsSeparatedByString(separator) } return inner }
  21. func separateBy(separator:String) -> ((String) -> [String]) { func inner(source:String) ->

    [String] { return source.componentsSeparatedByString(separator) } return inner } let lineSeparator = separateBy("\n") let commaSeparator = separateBy(",")
  22. 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