Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Alexander Voronov
Search
Oleksandr Voronov
September 15, 2015
Programming
2
1.2k
Alexander Voronov
Swift: Going Functional
https://youtu.be/Dc9aKhs37BE
Oleksandr Voronov
September 15, 2015
Tweet
Share
More Decks by Oleksandr Voronov
See All by Oleksandr Voronov
Controllable Randomness in Unit Tests
alexandervoronov
1
37
Modularizing your iOS apps
alexandervoronov
0
120
TDD in Real World
alexandervoronov
1
210
Building CLI with Swift
alexandervoronov
1
310
Stanfy MadCode 10: From Java to Kotlin, from Objective-C to Swift
alexandervoronov
0
220
ReactiveCocoa
alexandervoronov
0
140
Other Decks in Programming
See All in Programming
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
110
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
Criando Commits Incríveis no Git
marcelgsantos
2
170
CSC305 Lecture 26
javiergs
PRO
0
140
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
140
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
今からはじめるAndroidアプリ開発 2024 / DevFest 2024
star_zero
0
1k
Haze - Real time background blurring
chrisbanes
1
510
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
33
1.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Gamification - CAS2011
davidbonilla
80
5.1k
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Optimizing for Happiness
mojombo
376
70k
Building Adaptive Systems
keathley
38
2.3k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
510
The Cost Of JavaScript in 2023
addyosmani
45
7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
How GitHub (no longer) Works
holman
311
140k
Bash Introduction
62gerente
608
210k
Transcript
Swift Rocks 2! Alexander Voronov iOS Developer @ Stanfy
Swift Going Functional
What is Functional Programming?
Functional Programming
Functional Programming • Higher-order functions
Functional Programming • Higher-order functions • Immutable states & pure
functions
Functional Programming • Higher-order functions • Immutable states & pure
functions • Modularity
Functional Programming • Higher-order functions • Immutable states & pure
functions • Modularity • Types
Swift Power
Swift Power
Swift Power • First Class Functions
Swift Power • First Class Functions • Currying
Swift Power • First Class Functions • Currying • Generics
Swift Power • First Class Functions • Currying • Generics
• Type Inference
Swift Power • First Class Functions • Currying • Generics
• Type Inference • Enums
First Class Functions func add(x: Int) -> Int -> Int
{ return { y in y + x } } let addOne = add(1) addOne(2) // 3
First Class Functions func addTwo(x: Int) -> Int { return
x + 2 } (1...5).map(addTwo) // [3, 4, 5, 6, 7]
Currying func add(a: Int)(b: Int) -> Int { return a
+ b } let addOne = add(1) let xs = 1...5 xs.map(addOne) // [2, 3, 4, 5, 6]
Currying func curry<A, B, C>(f: (A, B) -> C) ->
A -> B -> C { return { a in { b in f(a, b) } } }
Generics func printEach<T: SequenceType>(items: T) { for item in items
{ print(item) } } printEach(1...5) printEach(["one", "two", "three"])
Type Inference let x = 42.0 x.dynamicType // Double x
is Double // true
Type Inference var xs = [1, 5, 2, 4, 3]
xs.sort(<) print(xs) // [1, 2, 3, 4, 5]
Type Inference var xs = [1, 5, 2, 4, 3]
xs.sort(<) print(xs) // [1, 2, 3, 4, 5]
Type Inference let xs = [1, 5, 2, 4, 3]
let ys = xs.sorted(<) print(xs) // [1, 5, 2, 4, 3] print(ys) // [1, 2, 3, 4, 5]
Type Inference let xs = [1, 5, 2, 4, 3]
let ys = xs.sorted(<) print(xs) // [1, 5, 2, 4, 3] print(ys) // [1, 2, 3, 4, 5]
Enumerations enum Fruit: String { case Apple = "apple" case
Banana = "banana" case Cherry = "cherry" } Fruit.Apple.rawValue // "apple"
Enumerations enum ValidationResult { case Valid case NotValid(NSError) }
Enumerations enum MyApi { case xAuth(String, String) case GetUser(Int) }
extension MyApi: MoyaTarget { var baseURL: NSURL { return NSURL(string: "")! } var path: String { switch self { case .xAuth: return "/authorise" case .GetUser(let id): return "/user/\(id)" } } } https://github.com/Moya/Moya
Optionals enum Optional<T> { case None case Some(T) } var
x = 5 x = nil // Error!
Optional Chaining struct Dog { var name: String } struct
Person { var dog: Dog? } let dog = Dog(name: "Dodge") let person = Person(dog: dog) let dogName = person.dog?.name
Optional Chaining struct Dog { var name: String } struct
Person { var dog: Dog? } let dog = Dog(name: "Dodge") let person = Person(dog: dog) let dogName = person.dog?.name Optional Chaining
Functors, Applicatives, Monads
None
Functors, Applicatives, Monads let x = 2 x + 3
// == 5
Functors, Applicatives, Monads let x = 2 x + 3
// == 5 let y = Optional(2)
Functors let y = Optional(2) y + 3 // Error!
// Value of optional type // Optional<Int> not unwrapped
Functors let y = Optional(2) y.map { $0 + 3
} // Optional(5)
Functors func map<U>(f: T -> U) -> U? { switch
self { case .Some(let x): return f(x) case .None: return .None } }
Functors infix operator <^> { associativity left } func <^><T,
U>(f: T -> U, a: T?) -> U? { return a.map(f) }
Functors func <^><T, U>(f: T -> U, a: T?) ->
U? { return a.map(f) } let addOne = { $0 + 1 } addOne <^> Optional(2) // Optional(3)
Applicative func apply<U>(f: (T -> U)?) -> U? { switch
f { case .Some(let someF): return self.map(someF) case .None: return .None } }
Applicatives infix operator <*> { associativity left } func <*><T,
U>(f: (T -> U)?, a: T?) -> U? { return a.apply(f) }
Applicatives infix operator <*> { associativity left } func <*><T,
U>(f: (T -> U)?, a: T?) -> U? { return a.apply(f) } func add(a: Int)(b: Int) -> Int { return a + b }
Applicatives add <^> Optional(2) <*> Optional(3) // Optional(5)
Applicatives add <^> Optional(2) <*> Optional(3) // Optional(5)
Applicatives add <^> Optional(2) <*> Optional(3) // Optional(5) let a
= add <^> Optional(2)
Applicatives add <^> Optional(2) <*> Optional(3) // Optional(5) let a
= add <^> Optional(2)
Applicatives add <^> Optional(2) <*> Optional(3) // Optional(5) let a
= add <^> Optional(2) let a: (b: Int) -> Int?
Monads typealias T = Double let f: T -> T
= { $0 * 2.0 } let g: (T, T) -> T = { $0 / $1 }
Monads typealias T = Double let f: T -> T
= { $0 * 2.0 } let g: (T, T) -> T = { $0 / $1 } f(g(4, 2))
Monads typealias T = Double let f: T -> T
= { $0 * 2.0 } let g: (T, T) -> T = { $0 / $1 } f(g(4, 2)) g: (T, T) -> T?
Monads typealias T = Double let f: T -> T
= { $0 * 2.0 } let g: (T, T) -> T = { $0 / $1 } f(g(4, 2)) g: (T, T) -> T? g(4, 2) >>- { f($0) }
Monads >>- == http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
Monads func flatten<U>(a: U??) -> U? { switch a {
case .Some(let someA): return someA case .None: return .None } }
Monads func flatMap<U>(f: T -> U?) -> U? { return
flatten(map(f)) }
Monads func flatMap<U>(f: T -> U?) -> U? { return
flatten(map(f)) } func map<U>(f: T -> U) -> U?
Monads func flatMap<U>(f: T -> U?) -> U? { return
flatten(map(f)) } func map<U>(f: T -> U) -> U?
Monads func flatMap<U>(f: T -> U?) -> U? { return
flatten(map(f)) } func map<U>(f: T -> U) -> U? func map<U?>(f: T -> U?) -> U??
Monads infix operator >>- { associativity left } func >>-<T,
U>(a: T?, f: T -> U?) -> U? { return a.flatMap(f) }
Monads func half(a: Int) -> Int? { return a %
2 == 0 ? a / 2 : .None }
Monads func half(a: Int) -> Int? { return a %
2 == 0 ? a / 2 : .None } Optional(8) >>- half >>- half // Optional(2)
Monad Laws • Left Identity • Right Identity • Associativity
Left Identity Law let f = { Optional($0 + 1)
} let a = 1 let x = Optional(a) >>- f let y = f(a) x == y
Right Identity Law func create<T>(value: T) -> T? { return
Optional(value) } let x = Optional(1) >>- create let y = Optional(1) x == y
Associativity Law let double = { Optional(2 * $0) }
let triple = { Optional(3 * $0) } let x = Optional(1) >>- double >>- triple let y = Optional(1) >>- { double($0) >>- triple } let z = { Optional(1) >>- double }() >>- triple x == y y == z
Recap Functor map<U>(f: T -> U) -> M<U> Applicative apply<U>(f:
M<(T -> U)>) -> M<U> Monad flatMap<U>(f: T -> M<U>) -> M<U>
Pipes & Railways @ScottWlaschin
Pipes infix operator |> { associativity left } public func
|> <T, U>(x: T, f: T -> U) -> U { return f(x) } let addTwo = { $0 + 2 } let prodThree = { $0 * 3 } 5 |> addTwo |> prodThree |> print // 21
Pipes let transformedX = x |> addTwo |> prodThree |>
increment |> square |> pow VS (pow(square(increment(prodThree(addTwo(x))))))
Railways http://fsharpforfunandprofit.com/posts/recipe-part2/
Argo JSON Parser + Argo Runes
Argo Example extension Model: Decodable { static func decode(json: JSON)
-> Decoded<Model> { return Model.create <^> json <| "id" <*> json <| "room" <*> json <| "guest_name" <*> json <| "status" <*> json <| "label" <*> json <|? "user_comment" <*> json <| ["channel", "label"] <*> json <| "severity" <*> json <|| "epochs" <*> json <|| "body" } }
Argo Example let entities: [Model]? entities = data?.json() >>- {
$0["entities"] } >>- decode
Where Next?
Where Next • Functors, Applicatives and Monads in Pictures •
Railway Oriented Programming • Functional Programming in Swift (Objc.io) • Argo • Swiftz • RxSwift • ReactiveCocoa-3.0 • Haskell, F#, Erlang, Elm
Thanks!
Comments / Questions? @aleks_voronov a-voronov