Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Dysfunctional Programming (phpnw13)
Igor Wiedler
October 05, 2013
Programming
3
670
Dysfunctional Programming (phpnw13)
Igor Wiedler
October 05, 2013
Tweet
Share
More Decks by Igor Wiedler
See All by Igor Wiedler
Wide Event Analytics (LISA19)
igorw
3
850
a day in the life of a request
igorw
0
86
production: an owner's manual
igorw
0
96
The Power of 2
igorw
0
160
LISP 1.5 Programmer's Manual: A Dramatic Reading
igorw
0
190
The Moral Character of Software
igorw
1
200
interdisciplinary computing (domcode)
igorw
0
170
miniKanren (clojure berlin)
igorw
1
180
End the war on tabs (phpnw14)
igorw
1
790
Other Decks in Programming
See All in Programming
Modern Android Developer ~ 안내서
pluu
1
620
マルチプロダクト×非構造化データ×機械学習を支えるデータ信頼性
akino
0
120
Licences open source : entre guerre de clochers et radicalité
pylapp
2
490
Managing Error Messages with your Oracle Database REST APIs
thatjeffsmith
0
120
GDG Seoul IO Extended 2022 - Android Compose
taehwandev
0
300
heyにおけるCI/CDの現状と課題
fufuhu
2
550
trocco® の品質を守る、とても普通な取り組み
kekekenta
0
360
Node-RED 3.0 新機能紹介
utaani
0
140
LINE Messaging APIの概要 - LINE API総復習シリーズ
uezo
1
170
GitHubのユーザー名を変更した後のあれこれ
tahia910
0
130
Mobile Product Engineering
championswimmer
0
300
ドメインモデル方式のクラス設計 座談会
masuda220
PRO
3
1k
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
324
55k
The World Runs on Bad Software
bkeepers
PRO
57
5.3k
Facilitating Awesome Meetings
lara
29
4k
We Have a Design System, Now What?
morganepeng
35
3k
Web development in the modern age
philhawksworth
197
9.3k
Become a Pro
speakerdeck
PRO
3
840
Why You Should Never Use an ORM
jnunemaker
PRO
47
7.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
316
22k
Debugging Ruby Performance
tmm1
65
10k
Ruby is Unlike a Banana
tanoku
91
9.2k
Practical Orchestrator
shlominoach
178
8.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
15
36k
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