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
590
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
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
550
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
470
ドメインイベント増えすぎ問題
h0r15h0
2
310
php-conference-japan-2024
tasuku43
0
290
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
350
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
380
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
330
The Efficiency Paradox and How to Save Yourself and the World
hollycummins
1
440
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
190
Featured
See All Featured
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Rails Girls Zürich Keynote
gr2m
94
13k
Music & Morning Musume
bryan
46
6.2k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
How to Ace a Technical Interview
jacobian
276
23k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Thoughts on Productivity
jonyablonski
67
4.4k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
For a Future-Friendly Web
brad_frost
175
9.4k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
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]