Share
Embed
Transcript
146 slides
-
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 -
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 -
What is Functional Programming*? (and how to use it in
Swift) *FP from now on… -
“A monad is a monoid in the category of endofunctors”
http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html -
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 -
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 -
• Side-Effect Free • Stateless • Immutable (the Data anyway)
Functional Programming (FP) An approach that Strives to be… http://bit.ly/AltConf2017 -
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’ */ } } -
OK Side Effect Mutate Data Change State When you think
of… A Button on a Screen Object -
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 } -
+ 2 2 = 4 Side Effect Mutate Data Change
State When you think of… Adding two numbers Function -
func squareANumber(x: Int) -> Int { return x * x
} let squareANumber = { (x: Int) in x * x } Function: Closure Expression: Closure Expression -
func squareANumber(x: Int) -> Int { return x * x
} let squareANumber = { (x: Int) in x * x } Function: Closure Expression: Function Literal -
Closure: Function with out a name a.k.a. Anonymous Function Function:
Closure with a name -
‘add’ Function Input 4 Output (2, 2) func add(_ lhs:
Int, _ rhs: Int) -> Int add(2, 2) -
The value of ALL VARIABLES in an Object* at any
instant in time http://bit.ly/AltConf2017 -
class ShapeClass { var shape: Shape var lineColor: Color var
fillColor: Color } 3 Choices 3 Choices 3 Choices 27 POSSIBLE STATES -
Once a value is set, it CAN NOT be changed.
http://bit.ly/AltConf2017 -
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 { } } -
• Side-Effect Free • Stateless • Uses Immutable Data Functional
Programming (FP) More or less(ish) http://bit.ly/AltConf2017 -
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 -
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’ -
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 -
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 -
func doSomething(x: Int, function: ((Int) -> Int)) -> Int {
Type: } return function(x) Type: Type: Closure -
} 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) -
} 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) -
} 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) -
} 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) -
func doWhat(x: Int) -> ((Int) -> Int) { Type: Closure
Type: } doWhat -
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 -
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 -
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 -
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 } -
Function take a Function as an argument Function return a
Function Closure Closure http://bit.ly/AltConf2017 -
Higher-Order Functions Function that takes a Function as an argument
Function that returns a Function Function that takes and returns a Function -
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) -
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 -
Map func map<T, U>( function: (T) -> U) -> [U]
let new = [1,2,3,4,5].map { 10 * $0 } -
Function [Array] [Results] { 10 * number } { 10
* number } { 10 * number } { 10 * number } { 10 * } { 10 * } { 10 * } { 10 * } { 10 * } 1 2 3 4 5 [ , , , , ] -
Function [Array] [Results] 10 { 10 * number } {
10 * number } { 10 * number } { 10 * number } { 10 * } { 10 * } { 10 * } { 10 * } { 10 * } = 1 2 3 4 5 [ , , , , ] -
Function [Array] [Results] 10 { 10 * } { 10
* } { 10 * } { 10 * } { 10 * } = 20 = 30 = 40 = 50 = 1 2 3 4 5 [ , , , , ] 10 20 30 40 50 -
Map func map<T, U>( function: (T) -> U) -> [U]
‘Generic’ Types -
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) -
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] -
T = Some Type func map<T, U>(function: (T) -> U)
-> [U] Int, Float, String, class, struct… Generic Types -
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 -
Filter func filter<T>(function: (T) -> Bool) -> [T] let new
= [1,2,3,4,5].filter { $0 % 2 == 0 } -
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 [ , ] -
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 -
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 -
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 [ , ] -
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 -
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 -
Filter let new = [1,2,3,4,5].filter { $0 % 2 ==
0 } func filter<T>(function: (T) -> Bool) -> [T] -
Filter let new = [1,2,3,4,5].filter { $0 % 2 ==
0 } // new = [2,4] func filter<T>(function: (T) -> Bool) -> [T] -
{ $0 + $1 } Reduce func reduce<T>( initialValue: T,
function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) -
{ $0 + $1 } Reduce func reduce<T>( initialValue: T,
function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) Acc. Acc. -
{ $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. -
{ $0 + $1 } Reduce func reduce<T>( initialValue: T,
function: (T, T) -> T) -> T let new = [1,2,3,4,5].reduce(0, + ) -
Reduce func reduce<T>( initialValue: T, function: (T, T) -> T)
-> T let new = [1,2,3,4,5].reduce(0, + ) -
Result [ , , , , ] 1 2 3
4 5 Function [Array] Accumulator Initial Value + } { x acc Acc Init -
Result [ , , , , ] 1 2 3
4 5 Function [Array] + } { x acc Acc Init = -
Result [ , , , , ] 1 2 3
4 5 Function [Array] + } { x acc = 0 0 -
Result + } 1 2 3 4 5 Function [Array]
{ x acc + } { x acc 0 -
{ $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. -
Reduce func reduce<T>( initialValue: T, function: (T, T) -> T)
-> T let new = [1,2,3,4,5].reduce(0, + ) // new = 15 -
Imperative vs. Declarative ‘How To Do Something’ vs. ‘What You
Want Done’ http://bit.ly/AltConf2017 -
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 -
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 -
let mapped = array.map { $0 * 10 } let
array = [1,2,3,4,5] -
let mapped = array.map { $0 * 10 } let
filtered = array.filter { $0 % 2 == 0 } let array = [1,2,3,4,5] -
let mapped = array.map { $0 * 10 } let
filtered = array.filter { $0 % 2 == 0 } let array = [1,2,3,4,5] -
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] -
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] -
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 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 } -
func addClosure(x: Int) -> ((Int) -> Int) { return {
y in x * y } } let add = addClosure(x: 10) add(10) // 100 x x -
func addClosure(x: Int) -> ((Int) -> Int) { return {
y in x * y } } let add = addClosure(x: 10) add(100) // 1000 add(10) // 100 x x -
((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 -
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