Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
560
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
56
Immutability to Save an Ever-Changing World
cassell
0
23
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
770
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
340
Domain-Driven Design
cassell
2
980
Other Decks in Programming
See All in Programming
しっかり学ぶ java.lang.*
nagise
1
470
チーム開発の “地ならし"
konifar
8
6.8k
FluorTracer / RayTracingCamp11
kugimasa
0
170
TypeScriptで設計する 堅牢さとUXを両立した非同期ワークフローの実現
moeka__c
6
2.8k
俺流レスポンシブコーディング 2025
tak_dcxi
13
6.8k
connect-python: convenient protobuf RPC for Python
anuraaga
0
340
無秩序からの脱却 / Emergence from chaos
nrslib
2
11k
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 1
philipschwarz
PRO
0
120
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
8
3.9k
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
260
CloudNative Days Winter 2025: 一週間で作る低レイヤコンテナランタイム
ternbusty
7
1.9k
スタートアップを支える技術戦略と組織づくり
pospome
8
14k
Featured
See All Featured
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
Code Review Best Practice
trishagee
73
19k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
How to Ace a Technical Interview
jacobian
280
24k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
31
2.7k
Visualization
eitanlees
150
16k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.5k
Done Done
chrislema
186
16k
Music & Morning Musume
bryan
46
7k
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