Practical Functional Programming (dotSwift 2015)

D200a17dd269fd4001bacb11662dab4b?s=47 Kyle Fuller
February 06, 2015

Practical Functional Programming (dotSwift 2015)

A practical introduction to functional programming in Swift and how it can be applied to the real world. Going over what being functional means and how it allows you to write clearer, declarative and testable code.

Usually, functional programming is taught by abstract functional techniques. Instead this talk goes for a different approach showing examples of unfunctional, imperative code people may have written in the past and how that can be improved with a functional style.

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

February 06, 2015
Tweet

Transcript

  1. 6.

    What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  2. 7.

    What makes up functional programming? 4 first class functions 4

    immutable data 4 reducing 4 pipelining 4 recursing 4 currying 4 monads
  3. 11.

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

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

    High Order Functions 4 Functions that take other functions as

    arguments 4 Functions that returns a functions
  5. 22.
  6. 33.

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

    = { value in println(“Hi \(value)”) } message2 = message1 message2(“dotSwift”) // Hello dotSwift
  7. 37.

    How many people are in each group? let groups =

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

    map

  9. 41.

    How many people are in each group? let groups =

    [["Kyle", "Maxine"], ["André", "Katie", "Ash"]] map(groups) { countElements($0) } // [2, 3]
  10. 45.

    How many people are in each group? let groups =

    [["Kyle", "Maxine"], ["André", "Katie", "Ash"]] map(groups, countElements) // [2, 3]
  11. 48.
  12. 52.

    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
  13. 57.

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

    "Maxine"], ["André", "Katie", "Ash"]] var people = [String]() for group in groups { people += group } // ["Kyle", "Maxine", "André", "Katie", "Ash"]
  14. 58.
  15. 63.

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

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

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

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

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

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

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

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

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

    [String] { return source.componentsSeparatedByString(separator) } return inner }
  20. 70.

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

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