map, reduce and filter in swift

34
Map, Reduce and Filter in Swift Aleksandras Smirnovas, coder.lt Cocoa.lt #2, 2015

Upload: aleksandras-smirnovas

Post on 26-Jan-2017

2.857 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Map, Reduce and Filter in Swift

Map, Reduce and Filter in Swift

Aleksandras Smirnovas, coder.lt Cocoa.lt #2, 2015

Page 2: Map, Reduce and Filter in Swift

Agenda

• What is functional programming?

• Swift 101

• Examples of unfunctional to functional code

Page 3: Map, Reduce and Filter in Swift

Functional Programming

is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

http://en.wikipedia.org/wiki/Functional_programming

Page 4: Map, Reduce and Filter in Swift

Functional Languages• Haskell

• Skala

• F#

• Erlang

• Clojure

Page 5: Map, Reduce and Filter in Swift

Key concepts• First-class and higher-order functions

• Pure functions

• Recursion

• Functional programming in non-functional languages

http://en.wikipedia.org/wiki/Functional_programming

Page 6: Map, Reduce and Filter in Swift

Swift 101: Function types

func sum(x: Int, y: Int) -> (Int) { return x + y }

Page 7: Map, Reduce and Filter in Swift

Swift 101: Function types

(Int, Int) -> (Int)

Page 8: Map, Reduce and Filter in Swift

Swift 101: Passing and returning functions

func calculator () -> ((Int, Int) -> Int) {

func sum(x: Int, y: Int) -> (Int) { return x + y }

return sum

} let sum = calculator()

sum(5, 6)

Page 9: Map, Reduce and Filter in Swift

Swift 101: Closure {()->() in}

{ (params) -> returnType in statements }

Page 10: Map, Reduce and Filter in Swift

Basic F-programming toolkit

• map

• reduce

• filter

Page 11: Map, Reduce and Filter in Swift

Map

func map<U>(transform: T -> U) -> Array<U>

Return an Array containing the results of calling `transform(x)` on each element x of self

Page 12: Map, Reduce and Filter in Swift

Map on collections

let list = ["c", "d", "a", "b"]

var uppercasedList = [String]()

for char in list { uppercasedList.append(char.uppercaseString) }

Page 13: Map, Reduce and Filter in Swift

Map on collections

let list = ["c", "d", "a", "b"]

let uppercasedList = list.map ({ (char: String) -> String in char.uppercaseString

})

Page 14: Map, Reduce and Filter in Swift

Map on collections

let list = ["c", "d", "a", "b"]

let uppercasedList = list.map ({$0.uppercaseString})

Page 15: Map, Reduce and Filter in Swift

Map on collections

let list = ["c", "d", "a", "b"]

let uppercasedList = list.map {$0.uppercaseString}

Page 16: Map, Reduce and Filter in Swift

FlatMap

func flatMap(transform: T -> [U]) ->[U]

Return an Array containing the results of calling `transform(x)` on each element x of flattening self

Page 17: Map, Reduce and Filter in Swift

FlatMap

let multiList = [["c", "d"], ["a"], ["b"]] var uppercasedMultiList = multiList .flatMap{ $0 } .map {$0.uppercaseString}

Page 18: Map, Reduce and Filter in Swift

Map on optionals

func increment(oNumber: Int?) -> Int? { if let number = oNumber { return number + 1 } else { return nil } }

increment(10) //11 increment(nil) // nil

Page 19: Map, Reduce and Filter in Swift

Map on optionals

func increment(oNumber: Int?) -> Int? { return oNumber.map { number in number + 1 } }

increment(10) //11 increment(nil) // nil

Page 20: Map, Reduce and Filter in Swift

Map on optionals

func increment(oNumber: Int?) -> Int? { return oNumber.map { $0 + 1 } }

increment(10) //11 increment(nil) // nil

Page 21: Map, Reduce and Filter in Swift

Reduce

func reduce(initial: U, combine: (U, T) -> U) -> U

Return the result of repeatedly calling `combine` with an accumulated value initialised to `initial` and each

element of self

Page 22: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

var sum = 0

for i in numbers { sum += i }

Page 23: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let sum = numbers.reduce(0, combine: { (total, number) in return total + number

})

Page 24: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let sum = numbers.reduce(0) { (total, number) in return total + number

}

Page 25: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let sum = numbers.reduce(0) { (total, number) in total + number

}

Page 26: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let sum = numbers.reduce(0) { $0 + $1 }

Page 27: Map, Reduce and Filter in Swift

Reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let sum = numbers.reduce(0, +}

Page 28: Map, Reduce and Filter in Swift

Filter

func filter(includeElement: T -> Bool) -> [T]

Return an Array containing the elements x of self for which `includeElement(x)` is true

Page 29: Map, Reduce and Filter in Swift

Filter

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let evenNumbers = numbers .filter { $0 % 2 == 0 }

Page 30: Map, Reduce and Filter in Swift

Map Filter reduce

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

let iEvenSum = numbers .map { $0 + 1 } .filter { $0 % 2 == 0 } .reduce(0, +)

Page 31: Map, Reduce and Filter in Swift

Further Topics

• Function Composition

• Custom operators

• Curried functions

• Functors, Applicative Functors, and Monads

Page 32: Map, Reduce and Filter in Swift

Further Topicstypealias Filter = CIImage -> CIImage

infix operator >>> { associativity left }

func >>> (filter1: Filter, filter2: Filter) -> Filter { return {img in filter2(filter1(img))} }

let myFilter = blur(blurRadius) >>> colorOverlay(overlayColor)

let result = myFilter(image)

http://www.objc.io/books/

Page 33: Map, Reduce and Filter in Swift

Resources• http://www.functionalvilnius.lt

• http://www.haskell.org/haskellwiki/Haskell

• http://learnyouahaskell.com

• http://www.objc.io/books/

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

Page 34: Map, Reduce and Filter in Swift

Thank you

Questions?

@saleksandras