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
Finding Happiness in Functional Programming
Search
Brandon Williams
October 01, 2016
Programming
0
600
Finding Happiness in Functional Programming
Video:
https://www.youtube.com/watch?v=A0VaIKK2ijM
Brandon Williams
October 01, 2016
Tweet
Share
More Decks by Brandon Williams
See All by Brandon Williams
Server-Side Swift from Scratch
mbrandonw
4
1.6k
Playground Driven Development
mbrandonw
0
320
Anything You Can Do I Can Do Better
mbrandonw
1
110
The Two Sides of Testing
mbrandonw
1
130
Other Decks in Programming
See All in Programming
Spring gRPC について / About Spring gRPC
mackey0225
0
220
AWS Organizations で実現する、 マルチ AWS アカウントのルートユーザー管理からの脱却
atpons
0
130
『GO』アプリ データ基盤のログ収集システムコスト削減
mot_techtalk
0
110
一休.com のログイン体験を支える技術 〜Web Components x Vue.js 活用事例と最適化について〜
atsumim
0
110
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
8
1.7k
密集、ドキュメントのコロケーション with AWS Lambda
satoshi256kbyte
0
180
Kubernetes History Inspector(KHI)を触ってみた
bells17
0
200
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
1
640
CloudNativePGがCNCF Sandboxプロジェクトになったぞ! 〜CloudNativePGの仕組みの紹介〜
nnaka2992
0
220
Ruby on cygwin 2025-02
fd0
0
140
第3回 Snowflake 中部ユーザ会- dbt × Snowflake ハンズオン
hoto17296
4
360
動作確認やテストで漏れがちな観点3選
starfish719
6
1k
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Adopting Sorbet at Scale
ufuk
74
9.2k
Rails Girls Zürich Keynote
gr2m
94
13k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
A Philosophy of Restraint
colly
203
16k
Visualization
eitanlees
146
15k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Thoughts on Productivity
jonyablonski
69
4.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
A better future with KSS
kneath
238
17k
Transcript
Finding Happiness in Functional Programming
None
Principles that we benefited from
Separation of effects from purity
Isolation of side-effects An expression is said to have a
side-effect if its execution makes an observable change to the outside world.
Isolation of side-effects self.titleLabel.text = user.name
Isolation of side-effects func update(text: String, forLabel: UILabel) { label.text
= text } update(text: user.name, forLabel: self.titleLabel)
Isolation of side-effects self.loginButton.enabled = !self.userTextField.text.isEmpty && !self.passwordTextField.text.isEmpty
Isolation of side-effects func updateLoginButtonEnabled() { self.loginButton.enabled = !self.userTextField.text.isEmpty &&
!self.passwordTextField.text.isEmpty } func emailChanged() { self.updateLoginButtonEnabled() } func passwordChanged() { self.updateLoginButtonEnabled() }
Isolation of side-effects // Pure, functional world let emailChanged: Signal<String,
NoError> let passwordChanged: Signal<String, NoError> let loginButtonEnabled = combineLatest(emailChanged, passwordChanged) .map { !$0.isEmpty && !$1.isEmpty } // Side-effect world loginButtonEnabled.observeNext { [weak self] in self?.loginButtonEnabled.enabled = $0 }
Isolation of side-effects // Pure, functional world let emailChanged: Signal<String,
NoError> let passwordChanged: Signal<String, NoError> let loginButtonEnabled = combineLatest(emailChanged, passwordChanged) .map { !$0.isEmpty && !$1.isEmpty } // Side-effect world self.loginButton.rac.enabled = loginButtonEnabled
Surfacing of co-effects
Surfacing of co-effects ????????????????
Surfacing of co-effects If an effect is a change to
the outside world after executing an expression...
Surfacing of co-effects If an effect is a change to
the outside world after executing an expression... ...then...
Surfacing of co-effects If an effect is a change to
the outside world after executing an expression... ...then... ...a co-effect is the state of the world that the expression needs in order to execute.
Surfacing of co-effects e.g. Dependency Injection
Surfacing of co-effects Dependency Injection func currentUserIsCreator(ofProject project: Project) ->
Bool { return User.currentUser.id == project.creator.id } currentUserIsCreator(ofProject: project) // => true or false
Surfacing of co-effects Dependency Injection func user(_ user: User, isCreatorOfProject:
Project) -> Bool { return user.id == project.creator.id } user(User.currentUser, isCreatorOfProject: project) // => true or false
Surfacing of co-effects References — Colin Barrett — Functional Swift
Conference 2015 — Structure and Interpretation of Swift Programs — The work of Tomas Petricek — Coeffects: A calculus of context-dependent computation — Coeffects: The next big programming challenge
Effect/Co-effect Duality
Code to the interface you wish you had, not the
interface you were given. - Stephen Celis
An interface we were given
An interface we were given Storyboards — Very thick abstraction
layer — Separates code from data — Constantly catching up to what UIKit can do
An interface we wish we had Lenses
An interface we wish we had Lenses struct Project {
let creator: User let id: Int let name: String }
An interface we wish we had Lenses Project.lens.name // =>
Lens<Project, String>
An interface we wish we had Lenses Project.lens.name // =>
Lens<Project, String> Project.lens.name .~ "Advanced Swift" // => Project -> Project
An interface we wish we had Lenses Project.lens.name // =>
Lens<Project, String> Project.lens.name .~ "Advanced Swift" // => Project -> Project project |> Project.lens.name .~ "Advanced Swift"
An interface we wish we had Lenses project |> Project.lens.name
.~ "Advanced Swift" |> Project.lens.creator.name .~ "Chris Eidhof"
An interface we wish we had UIKit Lenses
An interface we wish we had UIKit Lenses UIView.lens.backgroundColor //
=> Lens<UIView, UIColor>
An interface we wish we had UIKit Lenses UIView.lens.backgroundColor //
=> Lens<UIView, UIColor> UIView.lens.backgroundColor .~ .redColor() // => UIView -> UIView
An interface we wish we had UIKit Lenses UIView.lens.backgroundColor //
=> Lens<UIView, UIColor> UIView.lens.backgroundColor .~ .redColor() // => UIView -> UIView view |> UIView.lens.backgroundColor .~ .redColor() |> UIView.lens.layer.cornerRadius .~ 4 |> UIView.lens.layer.masksToBounds .~ true
An interface we wish we had UIKit Lenses func roundedStyle(cornerRadius:
CGFloat) -> (UIView) -> UIView { return UIView.lens.layer.cornerRadius .~ 4 <> UIView.lens.layer.masksToBounds .~ true } view |> roundedStyle(cornerRadius: 4) |> UIView.lens.backgroundColor .~ .redColor()
An interface we wish we had UIKit Lenses let baseButtonStyle
= roundedStyle(cornerRadius: 4) <> UIButton.lens.titleLabel.font .~ UIFont(size: 16) <> UIButton.lens.contentEdgeInsets .~ .init(topBottom: 6, leftRight: 12) let greenButtonStyle = baseButtonStyle <> UIButton.lens.backgroundColor(forState: .Normal) .~ .greenColor()
An interface we wish we had UIKit Lenses let bigButtonStyle
= baseButtonStyle <> UIButton.lens.contentEdgeInsets %~ { .init(top: $0.top * 2, left: $0.left * 2, bottom: $0.bottom * 2, right: $0.right * 2) }
An interface we wish we had UIKit Lenses let baseButtonStyle
= roundedStyle(cornerRadius: 4) <> UIButton.lens.titleLabel.font %~~ { _, button in button.traitCollection.verticalSizeClass == .Compact ? UIFont(size: 12) : UIFont(size: 14) } <> UIButton.lens.contentEdgeInsets .~ .init(topBottom: 6, leftRight: 12)
Principles that we did not benefit so much from: —
D.R.Y. — S.R.P. — S.O.L.I.D. — Objects
The Result
Testing
Test-Driven Development
Test-Driven Bug Fixing
Playground-Driven Development
Screenshot testing
Event Tracking
Event Tracking
Accessibility
Love for UIKit
Be!er working relationship with Product Managers, Designers and Engineers
Finding Happiness in Functional Programming
Finding Happiness in Functional Programming
[email protected]