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
120
Other Decks in Programming
See All in Programming
“あなた” の開発を支援する AI エージェント Bedrock Engineer / introducing-bedrock-engineer
gawa
4
420
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
410
Beyond ORM
77web
11
1.6k
歴史と現在から考えるスケーラブルなソフトウェア開発のプラクティス
i10416
0
300
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
1.9k
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
190
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
430
AHC041解説
terryu16
0
410
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
200
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
27
1.5k
Music & Morning Musume
bryan
46
6.3k
Fireside Chat
paigeccino
34
3.1k
How to Ace a Technical Interview
jacobian
276
23k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.6k
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Designing for humans not robots
tammielis
250
25k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
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]