Kyle Fuller
June 12, 2015
170

# 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.

June 12, 2015

## Transcript

22. ### What makes up functional programming? 4 first class functions 4

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

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

+= 1 }
27. ### Un-functional Function func increment() { let center = NSNotificationCenter.defaultCenter() let

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

1 }

arguments
31. ### High Order Functions 4 Functions that take other functions as

arguments 4 Functions that returns a functions

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

= { value in println(“Hi \(value)”) }
49. ### func message1(value:String) { println(“Hello \(value)”) } var message2:(String -> Void)

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

53. ### 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]

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

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

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

[["Kyle", "Katie"], ["André", "Maxine", "Ash"]] map(groups, count) // [2, 3]

= [2, 3]
68. ### 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

71. ### let count = [2, 3] sorted(count, >) // largest group

is [3, 2].first

73. ### 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"]

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

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

81. ### 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"]]
82. ### let input = "Kyle,Katie\nAndré,Maxine,Ash" let groups = map(input.componentsSeparatedByString("\n")) { line

in line.componentsSeparatedByString(",") } groups // [["Kyle", "Katie"], ["André", "Maxine", "Ash"]]
83. ### 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"]]

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

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

[String] { return source.componentsSeparatedByString(separator) } return inner } let lineSeparator = separateBy("\n") let commaSeparator = separateBy(",")
87. ### func separateBy(separator:String)(source:String) -> [String] { return source.componentsSeparatedByString(separator) } let lineSeparator

= separateBy("\n") let commaSeparator = separateBy(",")
88. ### let input = "Kyle,Katie\nAndré,Maxine,Ash" map(lineSeparator(input), commaSeparator) // [["Kyle", "Katie"], ["André",

"Maxine", "Ash"]]

96. ### 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