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
Dysfunctional Programming (phpnw13)
Search
Igor Wiedler
October 05, 2013
Programming
1.1k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dysfunctional Programming (phpnw13)
Igor Wiedler
October 05, 2013
More Decks by Igor Wiedler
See All by Igor Wiedler
Redis Bedtime Stories
igorw
1
380
Wide Event Analytics (LISA19)
igorw
4
950
a day in the life of a request
igorw
0
170
production: an owner's manual
igorw
0
200
The Power of 2
igorw
0
340
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
490
The Moral Character of Software
igorw
1
320
interdisciplinary computing (domcode)
igorw
0
330
miniKanren (clojure berlin)
igorw
1
340
Other Decks in Programming
See All in Programming
Detecting Compromised CI with eBPF and Cilium Tetragon
lizrice
0
250
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
240
VibeCodingからAgenticWorkflowへ
starfish719
0
800
まだ間に合う!今年の夏こそSchemeのマクロ展開器を完全理解!
omasanori
0
460
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
270
異なる設計思想のフレームワークを経験して得た学び
amekuhideki
1
330
源内ハンズオン概要編
hideg
0
210
バグを直したら useEffect が消えた
colorful12
3
700
楽しそうなつよつよエンジニアと目が死んでる僕/A brilliant engineer having a blast, and dead-eyed me.
3l4l5
2
240
為什麼你並不需要ViewModel / No, you don't need a ViewModel
lovee
1
510
typoなんかねぇよ
raspython3
0
490
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
400
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
9
970
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.9k
The Curse of the Amulet
leimatthew05
2
14k
Thoughts on Productivity
jonyablonski
76
5.3k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
870
Facilitating Awesome Meetings
lara
57
7.1k
WCS-LA-2024
lcolladotor
0
800
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
From π to Pie charts
rasagy
0
320
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Transcript
Dysfunctional Programming
@igorwesome
None
None
None
Immuteability: The property of functional programmers that prevents them from
shutting up about pure functional programming. – Reg Braithwaite @raganwald
dys·func·tion·al
php λ
Paradigms
procedural Object- Oriented functional logic
Imperative Declarative
Structure Time & State
Structure
None
‘sup?
None
it’s not about the money
• Message passing • Interfaces • Composition • (Not inheritance)
Actor model: Asynchronous
None
None
f g h
tools and materials
• Value semantics • Higher-order functions • Lazy evaluation
The Garbage Overfloweth
$numberCollection = new NumberCollection(); $numberParser = new SimpleNumberStringParser(); $firstParsedNumber =
$numberParser->parse('1'); $firstNumber = new SimpleNumber($firstParsedNumber); $firstNumberProxy = new CollectionItemNumberProxy($firstNumber); $numberCollection->add($firstNumberProxy); $secondParsedNumber = $numberParser->parse('1'); $secondNumber = new SimpleNumber($secondParsedNumber); $secondNumberProxy = new CollectionItemNumberProxy($secondNumber); $numberCollection->add($secondNumberProxy); $addition = new AdditionOperator('SimplePHPEasyPlus\Number\SimpleNumber'); $operation = new ArithmeticOperation($addition); $engine = new Engine($operation); $calcul = new Calcul($engine, $numberCollection); $runner = new CalculRunner(); $runner->run($calcul); $result = $calcul->getResult(); $numericResult = $result->getValue();
1 + 1
Composition
c a b a b
a b c
Higher-order functions
Dependency Injection
Inversion of control is really just a pretentious way of
saying “Taking an argument”. – Rúnar Óli @runarorama
Taking an argument
g g’
0 1 2 3 4 even? 0 2 4
0 1 2 3 4 #(+ 5 %) 5 7
9 6 8
3 6 7 9 + 0 9 25 3 16
acumulator
Datastructures
Number 42 String "Hello, cruel world." Map {:foo "bar", :baz
"qux"} List [:a :b :c] Set #{"Arthur Dent" "Ford Prefect"}
function is a means of abstraction
data is a means of abstraction
Structure Time & State
Time & State
Place-Oriented Programming
None
None
None
None
identity
new Foo("x") !== new Foo("x")
defensive copy
$users = new UserList(); $users->add(new User("Igor")); notify($users);
while (count($users)) { $user = $users->shift(); notifyUser($user); }
notify(clone $users)
observation
$user->getName()
$user["name"]
Value
Primitives
Value Object
date address money response request
serialization
History
Objects are forgetful
$sophie->setName("Joanna")
2013-09-13 01:01:47 Sophie 2013-09-13 01:02:25 Joanna 2013-09-13 01:05:31 Caroline
$joanna = $sophie->setName("Joanna")
Git
98ca9 34ac2 f30ab master Snapshot A Snapshot B Snapshot C
Epochal Time Model
v1 v2 v3 v4 f f f identity states events
Lazy evaluation
Iterator Generator Stream
a b c d a’ b’ c’ d’ consume
Order of execution becomes implicit
๏ filter ๏ map ๏ reduce
What about I/O?
Filesystem Database Network }is a place
Separate I/O from computation
None
What about testing?
But I heard static methods are untestable and evil
You cannot mock functions
Why do we mock?
Functions are fast
Impure parts are mocked by passing functions
my_beautiful_program(function () { return do_evil_shit(); })
Pure functions are inherently testable
But functions make code look awkward!
strlen(get_name(get_user($data)))
$data |> get_user |> get_name |> strlen
pipeline('get_user', 'get_name', 'strlen')
But PHP does not properly support functions!
use function igorw\edn\parse; $data = parse($edn);
PHP 5.6 will hopefully get function autoloading
Applicability of the functional paradigm
f g h i j k l
A foo foo foo foo B foo foo C foo
foo D foo foo E foo foo
Granularity
None
Queue
๏ Value semantics ๏ Higher-order functions ๏ Lazy evaluation
pure functions
None
Questions? • joind.in/{ 9471, 9472 } • github.com/igorw/edn • github.com/nikic/iter
• @igorwesome
References • The Paradigms of Programming by Robert W. Floyd
• Execution in the Kingdom of Nouns by Steve Yegge • The Value of Values by Rich Hickey • Are we there yet? by Rich Hickey