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

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.

Kyle Fuller

June 12, 2015
Tweet

More Decks by Kyle Fuller

Other Decks in Technology

Transcript

  1. How

  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. func message1(value:String) { println(“Hello \(value)”) } var message2:(String -> Void)

    = { value in println(“Hi \(value)”) } message2 = message1 message2(“AltConf”) // Hello AltConf
  7. 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]
  8. map

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

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

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

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

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

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

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