Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Practical Functional Programming (dotSwift 2015)

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.

Kyle Fuller

February 06, 2015
Tweet

More Decks by Kyle Fuller

Other Decks in Technology

Transcript

  1. What makes up functional programming? 4 first class functions 4

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

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

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

    arguments 4 Functions that returns a functions
  5. func message1(value:String) { println(“Hello \(value)”) } var message2:(String -> Void)

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

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

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

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

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

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

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

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