Slide 1

Slide 1 text

Swift Functional Programming @parisba

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Agenda • What is Swift • Why bother? • Installing Swift • Playgrounds/REPL • Functional Programming Fundamentals • Swift Functional Programming

Slide 6

Slide 6 text

This is mostly a “how Swift does it” talk

Slide 7

Slide 7 text

A quick overview. Not all-inclusive.

Slide 8

Slide 8 text

Swift?! • Similar to Rust, Perl 6, and a bunch of other languages… • Another trendy, hip, cool, modern programming language… • Actually pretty good…

Slide 9

Slide 9 text

Why does Swift matter?

Slide 10

Slide 10 text

Swift is open source. swift.org

Slide 11

Slide 11 text

Swift is easy to teach.

Slide 12

Slide 12 text

iOS.

Slide 13

Slide 13 text

http://blog.appannie.com/app-annie-2015-retrospective/ Annual App Downloads, 2013 - 2015

Slide 14

Slide 14 text

http://blog.appannie.com/app-annie-2015-retrospective/ Annual App Revenue, 2013 - 2015

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

let = ["","",""] for in { print() }

Slide 17

Slide 17 text

Swift is not really a functional language

Slide 18

Slide 18 text

Nothing like a pure functional language.

Slide 19

Slide 19 text

Ideals of functional programming are important

Slide 20

Slide 20 text

Installing Swift

Slide 21

Slide 21 text

Available on Linux and OS X (OS X installation: just download Xcode)

Slide 22

Slide 22 text

Binaries available for Ubuntu

Slide 23

Slide 23 text

$ sudo apt-get install clang libicu-dev Download a binary from https://swift.org/download/#linux; extract it $ tar xzf swift--.tar.gz $ export PATH=/path/to/usr/bin:"${PATH}" This creates a usr/ directory; add it to your path: Swift is now installed! Download the pre-requisites:

Slide 24

Slide 24 text

Useful Swifty Things

Slide 25

Slide 25 text

Playgrounds

Slide 26

Slide 26 text

$ swift Welcome to Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81). Type :help for assistance. 1> let text = "Don t worry if you are not computer" text: String = "Don t worry if you are not computer" 2> text.uppercaseString $R0: String = "DON T WORRY IF YOU ARE NOT COMPUTER" 3>

Slide 27

Slide 27 text

What does functional programming mean?

Slide 28

Slide 28 text

Functions

Slide 29

Slide 29 text

First-class functions

Slide 30

Slide 30 text

Higher-order functions

Slide 31

Slide 31 text

Pure functions

Slide 32

Slide 32 text

Pure functions have no side effects.

Slide 33

Slide 33 text

No I/O, no changing outside state.

Slide 34

Slide 34 text

Why does functional programming matter?

Slide 35

Slide 35 text

© GDC CC-BY 2.0

Slide 36

Slide 36 text

–John Carmack “No matter what language you work in, programming in a functional style provides benefits. You should do it whenever it is convenient, and you should think hard about the decision when it isn't convenient.”

Slide 37

Slide 37 text

yeah ok but WHY

Slide 38

Slide 38 text

“Pure functions have a lot of nice properties.”

Slide 39

Slide 39 text

• Thread safety • Reusability • Testability • Understandability • Maintainability

Slide 40

Slide 40 text

Important Language Support

Slide 41

Slide 41 text

A really nice type system.

Slide 42

Slide 42 text

First-class functions. See also: lambda calculus, Haskell, OCaml, Scala, and so on.

Slide 43

Slide 43 text

func add(num1: Int, _ num2 : Int) -> Int { return num1 + num2 }

Slide 44

Slide 44 text

func add(num1: Int, _ num2 : Int) -> Int { return num1 + num2 } var addNumbers : (Int,Int) -> Int

Slide 45

Slide 45 text

func add(num1: Int, _ num2 : Int) -> Int { return num1 + num2 } var addNumbers : (Int,Int) -> Int addNumbers = add

Slide 46

Slide 46 text

func add(num1: Int, _ num2 : Int) -> Int { return num1 + num2 } var addNumbers : (Int,Int) -> Int addNumbers = add addNumbers(1,2)

Slide 47

Slide 47 text

Closures

Slide 48

Slide 48 text

Inline first-class functions

Slide 49

Slide 49 text

var add = { (num1 : Int, num2 : Int) -> Int in return num1 + num2 }

Slide 50

Slide 50 text

Swift will infer as much type info as it can

Slide 51

Slide 51 text

var add = { (num1 : Int, num2 : Int) -> Int in return num1 + num2 }

Slide 52

Slide 52 text

var add = { (num1 : Int, num2 : Int) -> Int in return num1 + num2 }

Slide 53

Slide 53 text

var add = { (num1 : Int, num2 : Int) -> Int in return num1 + num2 }

Slide 54

Slide 54 text

var add : (Int,Int) -> Int add = { $0 + $1 }

Slide 55

Slide 55 text

var add : (Int,Int) -> Int add = (+)

Slide 56

Slide 56 text

Value capturing

Slide 57

Slide 57 text

typealias Adder = (Int) -> Int

Slide 58

Slide 58 text

typealias Adder = (Int) -> Int func makeAdder(number: Int) -> Adder { return { $0 + number } }

Slide 59

Slide 59 text

typealias Adder = (Int) -> Int func makeAdder(number: Int) -> Adder { return { $0 + number } } let addTwo = makeAdder(2)

Slide 60

Slide 60 text

typealias Adder = (Int) -> Int func makeAdder(number: Int) -> Adder { return { $0 + number } } let addTwo = makeAdder(2) addTwo(5) // 7

Slide 61

Slide 61 text

First-class functions means we get higher-order functions!

Slide 62

Slide 62 text

typealias TwoIntegerFunction = (Int,Int) -> Int

Slide 63

Slide 63 text

typealias TwoIntegerFunction = (Int,Int) -> Int func applyOperationToNumbers(num1 : Int, _ num2 : Int, operation: TwoIntegerFunction) -> Int { return operation(num1, num2) }

Slide 64

Slide 64 text

typealias TwoIntegerFunction = (Int,Int) -> Int func applyOperationToNumbers(num1 : Int, _ num2 : Int, operation: TwoIntegerFunction) -> Int { return operation(num1, num2) } let add : TwoIntegerFunction = { $0 + $1 }

Slide 65

Slide 65 text

typealias TwoIntegerFunction = (Int,Int) -> Int func applyOperationToNumbers(num1 : Int, _ num2 : Int, operation: TwoIntegerFunction) -> Int { return operation(num1, num2) } let add : TwoIntegerFunction = { $0 + $1 } applyOperationToNumbers(2, 3, operation: add)

Slide 66

Slide 66 text

typealias TwoIntegerFunction = (Int,Int) -> Int func applyOperationToNumbers(num1 : Int, _ num2 : Int, operation: TwoIntegerFunction) -> Int { return operation(num1, num2) } let add : TwoIntegerFunction = { $0 + $1 } applyOperationToNumbers(2, 3, operation: add) applyOperationToNumbers(2, 3, operation: +)

Slide 67

Slide 67 text

Optionals

Slide 68

Slide 68 text

In Swift, variables are never allowed to be nil, unless they’re Optionals.

Slide 69

Slide 69 text

var text = "As you might know, I am a full time Internet" text.uppercaseString // "AS YOU MIGHT KNOW, I AM A FULL TIME INTERNET"

Slide 70

Slide 70 text

let textFromDisk = loadTextFromDisk() textFromDisk.uppercaseString // Error!!

Slide 71

Slide 71 text

Very similar to Haskell’s Maybe

Slide 72

Slide 72 text

let textFromDisk = loadTextFromDisk() textFromDisk?.uppercaseString // "EVERYTHING HAPPENS SO MUCH"

Slide 73

Slide 73 text

Some Contrived Examples using Swift

Slide 74

Slide 74 text

func incrementArray(theArray: [Int]) -> [Int] { var result: [Int] = [] for item in theArray { result.append(item+1) } return result }

Slide 75

Slide 75 text

func doubleArray(theArray: [Int]) -> [Int] { var result: [Int] = [] for item in theArray { result.append(item*2) } return result }

Slide 76

Slide 76 text

typealias TransformFunction = Int -> Int func manipulateArray(theArray: [Int], transform: TransformFunction) -> [Int] { var result: [Int] = [] for item in theArray { result.append(transform(item)) } return result }

Slide 77

Slide 77 text

func doubleArray(xs: [Int]) -> [Int] { return manipulateArray(xs, transform:{ $0*2 }) }

Slide 78

Slide 78 text

Limitations…

Slide 79

Slide 79 text

[ ] , , , Int Int Int Int [ ] , , , Int Int Int Int

Slide 80

Slide 80 text

[ ] , , , Int Int Int Int [ ] , , , ? Bool Bool Bool Bool

Slide 81

Slide 81 text

Writing duplicate versions sucks

Slide 82

Slide 82 text

Generics!

Slide 83

Slide 83 text

func manipulateArray(theArray: [Int], transform: TransformFunction) -> [Int] { var result: [Int] = [] for item in theArray { result.append(transform(item)) } return result }

Slide 84

Slide 84 text

func map(xs: [Int], transform: Int->T) -> [T] { var result: [T] = [] for x in xs { result.append(transform(x)) } return result }

Slide 85

Slide 85 text

func map(xs: [Int], transform: Int->T) -> [T] { var result: [T] = [] for x in xs { result.append(transform(x)) } return result }

Slide 86

Slide 86 text

But we can take it further…

Slide 87

Slide 87 text

func map(xs: [Element], transform: Element->T) -> [T] { var result: [T] = [] for x in xs { result.append(transform(x)) } return result }

Slide 88

Slide 88 text

let doubledArray = map([1,2,3,4], transform: { $0 * 2} )

Slide 89

Slide 89 text

extension Array { func map(transform: (Element) -> T) -> [T] { var result: [T] = [] for x in self { result.append(transform(x)) } return result } }

Slide 90

Slide 90 text

[1,2,3,4].map({ $0 * 2 })

Slide 91

Slide 91 text

Map actually comes with Swift Hopefully you were expecting me to say that… … the point is, there’s no secrets to Map!

Slide 92

Slide 92 text

Composing functions

Slide 93

Slide 93 text

10 11 +1 ×2

Slide 94

Slide 94 text

5 10 11 +1 ×2

Slide 95

Slide 95 text

5 10 11 +1 ×2

Slide 96

Slide 96 text

5 10 11 +1 ×2

Slide 97

Slide 97 text

5 10 11 +1 ×2

Slide 98

Slide 98 text

×2 +1

Slide 99

Slide 99 text

11 ×2 +1

Slide 100

Slide 100 text

5 11 ×2 +1

Slide 101

Slide 101 text

5 11 ×2 +1

Slide 102

Slide 102 text

// Compose a function from two functions func compose(funcA: T->T, _ funcB: T->T) -> T->T { // Return a new function that calls them both return { funcB(funcA($0)) } }

Slide 103

Slide 103 text

// Compose a function from two functions func compose(funcA: T->T, _ funcB: T->T) -> T->T { // Return a new function that calls them both return { funcB(funcA($0)) } } // Two dumb lil functions let double = { $0 * 2 } let addOne = { $0 + 1 }

Slide 104

Slide 104 text

// Compose a function from two functions func compose(funcA: T->T, _ funcB: T->T) -> T->T { // Return a new function that calls them both return { funcB(funcA($0)) } } // Two dumb lil functions let double = { $0 * 2 } let addOne = { $0 + 1 } // Compose 'em! let doubleAndAddOne = compose(double, addOne)

Slide 105

Slide 105 text

// Compose a function from two functions func compose(funcA: T->T, _ funcB: T->T) -> T->T { // Return a new function that calls them both return { funcB(funcA($0)) } } // Two dumb lil functions let double = { $0 * 2 } let addOne = { $0 + 1 } // Compose 'em! let doubleAndAddOne = compose(double, addOne) doubleAndAddOne(5)

Slide 106

Slide 106 text

Limitations!

Slide 107

Slide 107 text

func compose(funcA: T->T, _ funcB: T->T) -> T->T These have to be the same type, for no good reason!

Slide 108

Slide 108 text

Is Even? Flip Boolean Bool Int Bool Bool

Slide 109

Slide 109 text

No Is Even? Flip Boolean

Slide 110

Slide 110 text

4 No Is Even? Flip Boolean

Slide 111

Slide 111 text

4 No Is Even? Flip Boolean

Slide 112

Slide 112 text

func compose(funcA: T->T, _ funcB: T->T) -> T->T { return { funcB(funcA($0)) } }

Slide 113

Slide 113 text

func compose(funcA: T->U, _ funcB: U->V) -> T->V { return { funcB(funcA($0)) } }

Slide 114

Slide 114 text

func compose(funcA: T->U, _ funcB: U->V) -> T->V { return { funcB(funcA($0)) } } let isEven = { $0 % 2 == 0 } // Int -> Bool let flipBool = { !$0 } // Bool -> Bool let isNotEven = compose(isEven, flipBool) // Int -> Bool isNotEven(4) // false

Slide 115

Slide 115 text

Third-party Stuff

Slide 116

Slide 116 text

Swiftz https://github.com/typelift/swiftz

Slide 117

Slide 117 text

Better types and tools for FP

Slide 118

Slide 118 text

import struct Swiftz.List // Cycles a finite list of numbers into an infinite list. let finite : List = [1, 2, 3, 4, 5] let infiniteCycle = finite.cycle() // Lists also support the standard map, filter, and reduce operators. let l : List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] let twoToEleven = l.map(+1) // [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] let even = l.filter((==0) • (%2)) // [2, 4, 6, 8, 10] let sum = l.reduce(curry(+), initial: 0) // 55 // Plus a few more. let partialSums = l.scanl(curry(+), initial: 0) // [0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55] let firstHalf = l.take(5) // [1, 2, 3, 4, 5] let lastHalf = l.drop(5) // [6, 7, 8, 9, 10]

Slide 119

Slide 119 text

Promissum https://github.com/tomlokhorst/Promissum

Slide 120

Slide 120 text

Dollar.swift dollarswift.org

Slide 121

Slide 121 text

Result https://github.com/antitypical/Result

Slide 122

Slide 122 text

Learning more • Learn You a Haskell for Great Good! • Pearls of Functional Algorithm Design • Programming in Haskell (Hutton) • Apple’s Swift Book(s) • IBM Swift Resources: https://developer.ibm.com/swift/

Slide 123

Slide 123 text

Thank you @parisba / http://www.secretlab.com.au