Practical Declarative Programming (AltConf 2015)

Practical Declarative Programming (AltConf 2015)

A practical introduction to declarative programming and how it can be applied to the real world. The talk will cover functional programming concepts, which allows you to write clearer, declarative and testable code. Usually, functional programming is taught by abstract functional techniques. Which are often hard to relate and understand the real benefits. Instead this talk goes for a different approach by showing examples of unfunctional, imperative code that people write every day and how we can translate that to a functional declarative style.

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

June 12, 2015
Tweet

Transcript

  1. 2.
  2. 3.
  3. 13.

    How

  4. 24.

    What makes up functional programming? 4 first class functions 4

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

    What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  6. 29.

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

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

    High Order Functions 4 Functions that take other functions as

    arguments 4 Functions that returns a functions
  8. 41.
  9. 52.

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

    = { value in println(“Hi \(value)”) } message2 = message1 message2(“AltConf”) // Hello AltConf
  10. 56.

    How many people are in each group? let groups =

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

    map

  12. 60.

    How many people are in each group? let groups =

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

    How many people are in each group? let groups =

    [["Kyle", "Katie"], ["André", "Maxine", "Ash"]] map(groups, count) // [2, 3]
  14. 67.
  15. 71.

    Order the numbers of people in each group let count

    = [2, 3] sorted(count) { (lhs, rhs) in lhs > rhs } // largest group is [3, 2].first
  16. 76.

    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"]
  17. 77.
  18. 82.

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

    "Katie"], ["André", "Maxine", "Ash"]] reduce(groups, [], +) // ["Kyle", "Katie", "André", "Maxine", "Ash"]
  19. 84.

    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"]]
  20. 85.

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

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

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

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

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

    [String] { return source.componentsSeparatedByString(separator) } return inner }
  23. 89.

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

    [String] { return source.componentsSeparatedByString(separator) } return inner } let lineSeparator = separateBy("\n") let commaSeparator = separateBy(",")
  24. 99.

    Conclusion 4 DSLs can be used to reduce bugs and

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