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
Obtaining Closure with Anonymous Functions
Search
Andrew Cassell
May 22, 2014
Programming
0
550
Obtaining Closure with Anonymous Functions
PHP Functional Programming using Functions, Lambdas, Anonymous Functions, and Closures.
Andrew Cassell
May 22, 2014
Tweet
Share
More Decks by Andrew Cassell
See All by Andrew Cassell
Immutability to Save an Ever-Changing World
cassell
0
54
Immutability to Save an Ever-Changing World
cassell
0
22
Domain-driven Design Workshop
cassell
1
280
Immutability to save an ever-changing world
cassell
3
530
Introduction to Domain-Driven Design in PHP - ZendCon & OpenEnterprise 2018
cassell
3
760
Immutability to Save an Ever-Changing World - PHP Benelux 2018
cassell
1
750
Domain-driven Design in PHP - PHP Benelux 2018
cassell
1
180
php[world] 2017 Domain-Driven Design Workshop
cassell
1
330
Domain-Driven Design
cassell
2
970
Other Decks in Programming
See All in Programming
Swift Concurrency 年表クイズ
omochi
3
220
なんでRustの環境構築してないのにRust製のツールが動くの? / Why Do Rust-Based Tools Run Without a Rust Environment?
ssssota
14
47k
SUZURIの規約違反チェックにおけるクリエイタフィードバックの試⾏錯誤/Trial and Error in Creator Feedback for SUZURI's Terms of Service Violation Checks
ae14watanabe
1
130
r2-image-worker
yusukebe
1
110
釣り地図SNSにおける有料機能の実装
nokonoko1203
0
210
AI駆動開発カンファレンスAutumn2025 _AI駆動開発にはAI駆動品質保証
autifyhq
0
130
CSC509 Lecture 08
javiergs
PRO
0
280
CSC305 Lecture 14
javiergs
PRO
0
240
開発組織の戦略的な役割と 設計スキル向上の効果
masuda220
PRO
10
2.1k
予防に勝る防御なし(2025年版) - 堅牢なコードを導く様々な設計のヒント / Growing Reliable Code PHP Conference Fukuoka 2025
twada
PRO
32
9.8k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
340
Researchlyの開発で参考にしたデザイン
adsholoko
0
110
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Testing 201, or: Great Expectations
jmmastey
46
7.8k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Faster Mobile Websites
deanohume
310
31k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.7k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.7k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Automating Front-end Workflow
addyosmani
1371
200k
Designing Experiences People Love
moore
142
24k
How STYLIGHT went responsive
nonsquared
100
5.9k
Transcript
Obtaining Closure with Anonymous Functions @alc277
Functions&! Lambdas&! Anonymous&! Closures&! Fun.
Named Functions
Named Functions
Functions&! Lambdas&! Anonymous&! Closures&! Fun.
NAMED LAMBDA
Lambda Functions
None
Lambda Functions
Lambda Functions
Type Hinting
Wow much useful such amaze ! very programmer
Functions&! Lambdas&! Anonymous&! Closures&! Fun.
None
Anonymous Functions
Functions&! Lambdas&! Anonymous&! Closures&! Fun.
λ Closures Rasmus’s Throne Room
None
Lambda Variable Scope
Closure Variable Scope & “use”
Tricky Bit
Passing a Variable to a Lambda or Closure
None
Passing a Variable by Reference to a Lambda or Closure
None
None
Closure Class
Closure! {! ! /* Methods */! ! public bindTo ()
{ }! ! ! ! ! ! ! ! } PHP Closure Class ! ! ! ! public static bind () {}! ! ! ** double-secret function __invoke() { }! !
None
Reasons to use Closures • Variable Scope and Binding! •
Don’t Pollute Namespace With Functions! • Map/Reduce/Filter! • Data Output! • Retrieving Records from Database
• Currying! • Fixed-point Combinators (Y- Combinator)! • Improved Performance
(Memoization)! • Monads! • Dependency Injection Reasons to use Closures
Things to Avoid • create_function! • Closure::bind()
Map/Reduce/Filter
Map/Reduce/Filter with Lambda Credit: Fabien Potencier
Map/Reduce/Filter Credit: Fabien Potencier Map/Reduce/Filter with Closure
Currying Photo: Penn State Office of Research Communications.
Currying
DATA OUTPUT
Data Output
Data Output
Database Records
Data Output
Y-Combinator
Y-Combinator
Y-Combinator
None
Memoization
fib(5) = fib(3) + fib(4)! fib(3) = fib(1) + fib(2)!
fib(1) = 1! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(3) = 1 + 1 = 2! fib(4) = fib(3) + fib(2)! fib(3) = fib(1) + fib(2)! fib(1) = 1! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(3) = 1 + 1 = 2! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(4) = 2 + 1 = 3! fib(5) = 3 + 2 = 5
Memoization
1 1 2 1 3 2 4 3 5 5
6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610 16 987
Memoization
None
Monads
MonadPHP Anthony Ferrara
Monads Credit: Anthony Ferrara
Monads Credit: Anthony Ferrara
Monads Credit: Anthony Ferrara
$posts = [! [“title" => "foo", "author" => [“name" =>
"Bob", "email" => “
[email protected]
”]],! [“title" => "bar", "author" => array("name" => "Tom", "email" => “
[email protected]
”]],! [“title" => “baz"],! [“title" => "biz", "author" => array("name" => "Mark", "email" => “
[email protected]
”]],! ]; Monads Credit: Anthony Ferrara
Monads Credit: Anthony Ferrara
Dependency! Injection
Dependency Injection
Dependency Injection
Dependency Injection
Dependency Injection
Dependency Injection