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

Functional Techniques in Swift

Functional Techniques in Swift

Kartik Patel

October 29, 2015
Tweet

More Decks by Kartik Patel

Other Decks in Programming

Transcript

  1. Immutable List let immutableList1 = [Int]() let immutableList2 = immutableList1

    + [1] let immutableList3 = immutableList2 + [2] let immutableList4 = immutableList3 + [3] or let immutableList1 = [Int]() let immutableList2 = immutableList1 + [1, 2, 3]
  2. Immutable List - Take 2 func add(list: [Int], _ num:

    Int) -> [Int] { return list + [num]; } let immutableList = add(add(add([Int](), 1), 2), 3);
  3. Immutable List - Take 3 extension Array { func add(newElement:

    Element) -> Array<Element> { return self + [newElement]; } } let immutableList = [Int]().add(1).add(2).add(3);
  4. Nullability let planets = ["Mercury": 3.7, "Venus": 8.9, "Earth": 9.8,

    "Mars": 3.7, "Jupiter": 23.1, "Saturn": 9.0, "Uranus": 8.7, "Neptune": 11] let jupiterGravity: Double = planets["Jupiter"] //error: value of optional type 'Double?' not unwrapped let jupiterGravity: Double? = planets["Jupiter"] if jupiterGravity != nil { print("The gravity on Jupiter is " + "\(jupiterGravity!)") } else { print("Not a planet: Jupiter") } let plutoGravity: Double? = planets["Pluto"] if plutoGravity != nil { print("The gravity on Pluto is " + "\(plutoGravity!)") } else { print("Not a planet: Pluto") }
  5. Nullability - Take 2 let planets = ["Mercury": 3.7, "Venus":

    8.9, "Earth": 9.8, "Mars": 3.7, "Jupiter": 23.1, "Saturn": 9.0, "Uranus": 8.7, "Neptune": 11] if let jupiterGravity = planets["Jupiter"] { print("The gravity on Jupiter is " + "\(jupiterGravity)") } else { print("Not a planet: Jupiter") } if let plutoGravity = planets["Pluto"] { print("The gravity on Pluto is " + "\(plutoGravity)") } else { print("Not a planet: Pluto") }
  6. Nullability - Optional Chaining1 struct Planet { let name :

    String let orbit: Orbit? } struct Orbit { let period: TimePeriod? let velocity: Double } struct TimePeriod { let value: Double? let units: String } planet.orbit!.period!.value! if let orbitPeriodValue = planet.orbit?.period?.value { ... } else { ... } 1 See Networking with Monads by John Gallagher @ Function Swift Conference 2014 http://2014.funswiftconf.com/speakers/ john.html
  7. Functions func dropWhile<T>(test: T -> Bool, list: [T]) -> [T]

    { let result: [T]; if let head = list.first { result = test(head) ? dropWhile(test, list:Array(list.dropFirst())) : list } else { result = list; } return result; } func lessThan5(i: Int) -> Bool { return i < 5; } dropWhile(lessThan5, list: [1,2,3,4,5,1,2,3])
  8. Functions func dropWhile<T>(test: T -> Bool) -> ([T]-> [T]) {

    func inner(list: [T]) -> [T] { let result: [T]; if let head = list.first { result = test(head) ? inner(Array(list.dropFirst())) : list } else { result = list; } return result; } return inner; } func lessThan5(i: Int) -> Bool { return i < 5; } let f = dropWhile(lessThan5) f([1,2,3,4,5,1,2,3]) f([1,1,2,3,5,8,13])
  9. Functions func isSpace(i: String) -> Bool { return i ==

    " "; } let g = dropWhile(isSpace) g([" ", "a"]) [Array(arrayLiteral:" a"), Array(arrayLiteral:"f"), Array(arrayLiteral:" e")].map(g)
  10. Resources Functional Programming in Swift By Chris Eidhof, Florian Kugler,

    and Wouter Swierstra https://www.objc.io/books/fpinswift/ Realm's News https://realm.io/news/ Youtube https://www.youtube.com