James Majors: What the Swiftly Func?

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
June 08, 2017

James Majors: What the Swiftly Func?

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

June 08, 2017
Tweet

Transcript

  1. 2.

    This presentation doesn't contain any confidential information and isn’t intended

    only for anyone who knows anything. Any other distribution, re-transmission, copying or disclosure of this message is highly encouraged. If you have received this transmission in error, just take a few deep breaths and relax, it’s really not a big deal. If you like it, pass it on, if you don’t like it, pass it on. Thanks! What the Swiftly Func? ALTCONF 2017 Presentation Created For Anyone Who Will Listen! Created By James Majors, iOS Developer http://bit.ly/AltConf2017
  2. 3.

    func iAm(_ who: Person?) -> Details { typealias Details =

    String guard let me = who else { return “Not Me!” } let whatIDo = “-> iOS Developer ” let atWhere = “@ POSSIBLE Mobile” return “\(me) \(whatIDo) \(atWhere)” } typealias Person = String let whoIAm: Person? = iAm(“James Majors”) // James Majors -> iOS Developer @ POSSIBLE Mobile
  3. 8.

    “A monad is a monoid in the category of endofunctors”

    http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html
  4. 9.

    Functional Programming Topics • Airity • Partial Application • Idempotent

    • Functor • Lift • Referential Transparency • Lambda Calculus • Monoid • Monad • Applicative Functor • Semigroup • Setoid https://github.com/hemanth/functional-programming-jargon utm_source=hashnode.com
  5. 10.

    What’s Coming Up? • Simple Definition(s) • Differences between other

    Approaches • Explain what some of the terms mean • Example of a ‘Functional’ Drawing Engine http://bit.ly/AltConf2017
  6. 11.
  7. 13.
  8. 14.
  9. 15.
  10. 16.

    • Side-Effect Free • Stateless • Immutable (the Data anyway)

    Functional Programming (FP) An approach that Strives to be… http://bit.ly/AltConf2017
  11. 19.
  12. 20.

    Object-Oriented Programming • Basic Building Block = Object (variables +

    functions) class SomeClass: NSObject { var aProperty: String func doSomething() { /* Transform ‘aProperty’ */ } } • Uses LOCAL variables (state) • Changing variables (mutable data) changes state • Side-Effects are usually how things get done class SomeClass: NSObject { var aProperty: String func doSomething() { /* Transform ‘aProperty’ */ } }
  13. 21.

    OK Side Effect Mutate Data Change State When you think

    of… A Button on a Screen Object
  14. 22.
  15. 23.

    Functional Programming • Basic Building Block = Function (stateless) func

    function(value: Type) -> Type { let newValue = /* Transform ‘value’ */ return newValue } • Values (state) are passed INTO the Function • State is represented by immutable data • Side-Effects are don’t happen (Highly discouraged) func function(value: Type) -> Type { let newValue = /* Transform ‘value’ */ return newValue }
  16. 24.

    + 2 2 = 4 Side Effect Mutate Data Change

    State When you think of… Adding two numbers Function
  17. 27.

    func squareANumber(x: Int) -> Int { return x * x

    } let squareANumber = { (x: Int) in x * x } Function: Closure Expression: Closure Expression
  18. 28.

    func squareANumber(x: Int) -> Int { return x * x

    } let squareANumber = { (x: Int) in x * x } Function: Closure Expression: Function Literal
  19. 37.
  20. 39.

    The value of ALL VARIABLES in an Object* at any

    instant in time http://bit.ly/AltConf2017
  21. 40.

    class ShapeClass { var shape: Shape var lineColor: Color var

    fillColor: Color } 3 Choices 3 Choices 3 Choices 27 POSSIBLE STATES
  22. 43.
  23. 48.

    let numbersArray: [Int] init(numbers: [Int]) { numbersArray = numbers }

    func add(number: Int) -> NumbersStruct { var newArray = Array(numbersArray) newArray.append(number) return NumbersStruct(numbers: newArray) } struct NumbersStruct { } }
  24. 50.
  25. 51.
  26. 53.

    • Side-Effect Free • Stateless • Uses Immutable Data Functional

    Programming (FP) More or less(ish) http://bit.ly/AltConf2017
  27. 55.

    func squareANumber(x: Int) -> Int { return x * x

    } let squareAnInt = { (x: Int) in x * x } let squareInt = squareAnInt Type: Closure Type: Closure let aNumber = 25
  28. 56.

    func squareANumber(x: Int) -> Int { return x * x

    } func doSomething(x: Int, function: ((Int) -> Int)) -> Int { -> Int { Type: Closure Type: func doubleANumber(x: Int) -> Int { return x + x } } return function(x) Type: Apply Function to ‘x’
  29. 57.

    func squareANumber(x: Int) -> Int { return x * x

    } func doSomething(x: Int, function: ((Int) -> Int)) -> Int { -> Int { func doubleANumber(x: Int) -> Int { return x + x } } return function(x) doSomething(x: 10, function: squareANumber) // 100
  30. 58.

    func squareANumber(x: Int) -> Int { return x * x

    } func doSomething(x: Int, function: ((Int) -> Int)) -> Int { -> Int { func doubleANumber(x: Int) -> Int { return x + x } } return function(x) doSomething(x: 10, function: squareANumber) // 100 doSomething(x: 10, function: doubleANumber) // 20
  31. 59.

    func doSomething(x: Int, function: ((Int) -> Int)) -> Int {

    Type: } return function(x) Type: Type: Closure
  32. 60.

    } x * x func doSomething(x: Int, function: ((Int) ->

    Int)) -> Int { Type: Closure Type: } return function(x) let doSomething(x: 10 { (x: Int) in ) , function: Type: Type: Closure return func squareANumber(x: Int) -> Int { return x * x } doSomething(x: 10, function: squareANumber)
  33. 61.

    } x * x func doSomething(x: Int, function: ((Int) ->

    Int)) -> Int { Type: } return function(x) let doSomething(x: 10 { (x: Int) in ) Type: Type: Closure return func squareANumber(x: Int) -> Int { return x * x } doSomething(x: 10, function: squareANumber)
  34. 62.

    } x * x func doSomething(x: Int, function: ((Int) ->

    Int)) -> Int { Type: } return function(x) let doSomething(x: 10 { (x: Int) in ) Type: Type: Closure func squareANumber(x: Int) -> Int { return x * x } doSomething(x: 10, function: squareANumber)
  35. 63.

    } x * x func doSomething(x: Int, function: ((Int) ->

    Int)) -> Int { Type: } return function(x) let doSomething(x: 10 // = 100 { (x: Int) in ) Type: Type: Closure Trailing Closure Syntax func squareANumber(x: Int) -> Int { return x * x } doSomething(x: 10, function: squareANumber)
  36. 65.

    func squareANumber(x: Int) -> Int { return x * x

    } func doWhat(x: Int) -> ((Int) -> Int) { func doubleANumber(x: Int) -> Int { return x + x } } Inner Functions doWhat
  37. 66.

    func squareANumber(x: Int) -> Int { return x * x

    } func doWhat(x: Int) -> ((Int) -> Int) { func doubleANumber(x: Int) -> Int { return x + x } } if x % 2 == 0 { return squareANumber } else { return doubleANumber } doWhat squareANumber doubleANumber
  38. 67.

    func doWhat(x: Int) -> ((Int) -> Int) { } if

    x % 2 == 0 { return squareANumber } else { return doubleANumber } let squareANumber = { (x: Int) in x * x } let doubleANumber = { (x: Int) in x + x } doWhat { (x: Int) in x + x } { (x: Int) in x * x } squareANumber doubleANumber
  39. 68.

    squareIt(5) // = 25 func doWhat(x: Int) -> ((Int) ->

    Int) { } if x % 2 == 0 { return squareANumber } else { return doubleANumber } let squareIt = doWhat(x: 2) let doubleIt = doWhat(x: 3) doubleIt(5) // = 10 doWhat { (x: Int) in x + x } { (x: Int) in x * x }
  40. 69.

    Function take a Function as an argument Function return a

    Function Closure Closure http://bit.ly/AltConf2017
  41. 71.

    Higher-Order Functions Function that takes a Function as an argument

    Function that returns a Function Function that takes and returns a Function
  42. 72.

    IntFunc func takeFunction(x: Int, function: return function(x) } func returnFunction(x:

    Int) -> if x % 2 == 0 { return { (x: Int) in x + x } } else { return { (x: Int) in x * x } } } typealias IntFunc = (Int) -> Int ) -> Int { { ((Int) -> Int)
  43. 73.

    IntFunc func takeFunction(x: Int, function: return function(x) } func returnFunction(x:

    Int) -> if x % 2 == 0 { return { (x: Int) in x + x } } else { return { (x: Int) in x * x } } } typealias IntFunc = (Int) -> Int ) -> Int { { IntFunc
  44. 76.

    Map func map<T, U>( function: (T) -> U) -> [U]

    let new = [1,2,3,4,5].map { 10 * $0 }
  45. 77.

    Function [Array] [Results] { 10 * number } { 10

    * number } { 10 * number } { 10 * number } { 10 * } { 10 * } { 10 * } { 10 * } { 10 * } 1 2 3 4 5 [ , , , , ]
  46. 78.

    Function [Array] [Results] 10 { 10 * number } {

    10 * number } { 10 * number } { 10 * number } { 10 * } { 10 * } { 10 * } { 10 * } { 10 * } = 1 2 3 4 5 [ , , , , ]
  47. 79.

    Function [Array] [Results] 10 { 10 * } { 10

    * } { 10 * } { 10 * } { 10 * } = 20 = 30 = 40 = 50 = 1 2 3 4 5 [ , , , , ] 10 20 30 40 50
  48. 81.

    Map func map<T, U>( function: (T) -> U) -> [U]

    let new = [1,2,3,4,5].map { 10 * $0 } Positional Argument (i.e. Function’s first Argument)
  49. 82.

    Map func map<T, U>( function: (T) -> U) -> [U]

    let new = [1,2,3,4,5].map { 10 * $0 } // new = [10,20,30,40,50]
  50. 94.

    T = Some Type func map<T, U>(function: (T) -> U)

    -> [U] Int, Float, String, class, struct… Generic Types
  51. 95.

    T = Some Type U = Some Type func map<T,

    U>(function: (T) -> U) -> [U] Int, Float, String, class, struct… Int, Float, String, class, struct… Generic Types
  52. 97.
  53. 98.

    Function [Array] [Results] { x % 2 == 0 }

    { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } 1 2 3 4 5 [ , ]
  54. 99.

    Function [Array] [Results] { x % 2 == 0 }

    { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } 2 3 4 5 [ , ] 1
  55. 100.

    Function [Array] [Results] false { x % 2 == 0

    } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } = 2 3 4 5 [ , ] 1
  56. 101.

    Function [Array] [Results] { x % 2 == 0 }

    { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } 2 3 4 5 [ , ]
  57. 102.

    Function [Array] [Results] { x % 2 == 0 }

    { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } 3 4 5 [ , ] 2
  58. 103.

    Function [Array] [Results] { x % 2 == 0 }

    { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } { x % 2 == 0 } true = 3 4 5 [ , ] 2
  59. 105.
  60. 107.

    Filter let new = [1,2,3,4,5].filter { $0 % 2 ==

    0 } func filter<T>(function: (T) -> Bool) -> [T]
  61. 108.

    Filter let new = [1,2,3,4,5].filter { $0 % 2 ==

    0 } // new = [2,4] func filter<T>(function: (T) -> Bool) -> [T]
  62. 110.
  63. 112.

    { $0 + $1 } Reduce func reduce<T>( initialValue: T,

    function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + )
  64. 113.

    { $0 + $1 } Reduce func reduce<T>( initialValue: T,

    function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) Acc. Acc.
  65. 114.

    { $0 + $1 } Reduce func reduce<T>( initialValue: T,

    function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) [Element] Acc. [Element] Acc.
  66. 115.

    { $0 + $1 } Reduce func reduce<T>( initialValue: T,

    function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + )
  67. 116.

    Reduce func reduce<T>( initialValue: T, function: (T, T) -> T)

    -> T let new = [1,2,3,4,5].reduce(0, + )
  68. 117.

    Result [ , , , , ] 1 2 3

    4 5 Function [Array] Accumulator Initial Value + } { x acc Acc Init
  69. 118.

    Result [ , , , , ] 1 2 3

    4 5 Function [Array] + } { x acc Acc Init =
  70. 119.

    Result [ , , , , ] 1 2 3

    4 5 Function [Array] + } { x acc = 0 0
  71. 120.
  72. 122.

    { $0 + $1 } Reduce func reduce<T>( initialValue: T,

    function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) [Element] Acc. [Element] Acc.
  73. 123.

    Reduce func reduce<T>( initialValue: T, function: (T, T) -> T)

    -> T let new = [1,2,3,4,5].reduce(0, + ) // new = 15
  74. 124.
  75. 126.

    func arrayTimesTen(array: [Int]) -> [Int] { var outputArray = [Int]()

    for number in array { var newNumber = number * 10 outputArray.append(newNumber) } return outputArray } var outputArray = [Int]() for number in array { var newNumber = number * 10 outputArray.append(newNumber) } let newNumber = number * 10 outputArray.append(newNumber) return outputArray number * 10
  76. 127.

    func arrayTimesTen(array: [Int]) -> [Int] { var outputArray = [Int]()

    for number in array { var newNumber = number * 10 outputArray.append(newNumber) } return outputArray } var outputArray = [Int]() for number in array { var newNumber = number * 10 outputArray.append(newNumber) } let newNumber = number * 10 outputArray.append(newNumber) return outputArray number * 10
  77. 129.
  78. 130.

    let mapped = array.map { $0 * 10 } let

    filtered = array.filter { $0 % 2 == 0 } let array = [1,2,3,4,5]
  79. 131.

    let mapped = array.map { $0 * 10 } let

    filtered = array.filter { $0 % 2 == 0 } let array = [1,2,3,4,5]
  80. 132.

    let mapped = array.map { $0 * 10 } let

    filtered = array.filter { $0 % 2 == 0 } let reduced = array.reduce(0, + ) let array = [1,2,3,4,5]
  81. 133.

    let mapped = array.map { $0 * 10 } let

    filtered = array.filter { $0 % 2 == 0 } let reduced = array.reduce(0, + ) let array = [1,2,3,4,5]
  82. 135.

    let mapIt = [1,2,3,4,5].map(timesTen) let filterIt = [1,2,3,4,5].filter(isEven) func timesTen(x:

    Int) -> Int { return x * 10 } func isEven(x: Int) -> Bool { return x % 2 == 0 }
  83. 136.

    let mapIt = [1,2,3,4,5].map(timesTen) let filterIt = [1,2,3,4,5].filter(isEven) func timesTen(x:

    Int) -> Int { return x * 10 } func isEven(x: Int) -> Bool { return x % 2 == 0 } let addIt = [1,2,3,4,5].reduce(0, add) func add(x: Int, y: Int) -> Int { return x + y }
  84. 140.

    func addClosure(x: Int) -> ((Int) -> Int) { return {

    y in x * y } } let add = addClosure(x: 10) add(10) // 100 x x
  85. 141.

    func addClosure(x: Int) -> ((Int) -> Int) { return {

    y in x * y } } let add = addClosure(x: 10) add(100) // 1000 add(10) // 100 x x
  86. 142.

    ((col or: Color) -> DrawingItem) func itemWith(shape: Shape, color: Color)

    -> DrawingItem { return DrawingItem(shape: shape, color: color) } func someShape(shape: Shape) -> } (_ color: Color) -> DrawingItem { return { color in return itemWith(shape: shape, color: color) } shape color shape color { } let ovalShape = someShape(shape: .oval) let triangleShape = someShape(shape: .triangle) let blackOval = ovalShape(.black) let whiteTriangle = triangleShape(.white) return itemWith(shape: shape, color: color) itemWith
  87. 145.

    This presentation doesn't contain any confidential information and isn’t intended

    only for anyone who knows anything. Any other distribution, re-transmission, copying or disclosure of this message is highly encouraged. If you have received this transmission in error, just take a few deep breaths and relax, it’s really not a big deal. If you like it, pass it on, if you don’t like it, pass it on. Thanks! What the Swiftly Func? ALTCONF 2017 Presentation Created For Anyone Who Will Listen! Created By James Majors, iOS Developer http://bit.ly/AltConf2016