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
310
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
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
初めてDefinitelyTypedにPRを出した話
syumai
0
410
flutterkaigi_2024.pdf
kyoheig3
0
130
距離関数を極める! / SESSIONS 2024
gam0022
0
280
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
Remix on Hono on Cloudflare Workers
yusukebe
1
290
Click-free releases & the making of a CLI app
oheyadam
2
120
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Contemporary Test Cases
maaretp
0
140
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Making Projects Easy
brettharned
115
5.9k
It's Worth the Effort
3n
183
27k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Site-Speed That Sticks
csswizardry
0
25
What's in a price? How to price your products and services
michaelherold
243
12k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Building Adaptive Systems
keathley
38
2.3k
Faster Mobile Websites
deanohume
305
30k
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]