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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Andrew Cassell
May 22, 2014
Programming
560
0
Share
Obtaining Closure with Anonymous Functions
PHP Functional Programming using Functions, Lambdas, Anonymous Functions, and Closures.
Andrew Cassell
May 22, 2014
More Decks by Andrew Cassell
See All by Andrew Cassell
Immutability to Save an Ever-Changing World
cassell
0
67
Immutability to Save an Ever-Changing World
cassell
0
46
Domain-driven Design Workshop
cassell
1
300
Immutability to save an ever-changing world
cassell
3
570
Introduction to Domain-Driven Design in PHP - ZendCon & OpenEnterprise 2018
cassell
3
810
Immutability to Save an Ever-Changing World - PHP Benelux 2018
cassell
1
1k
Domain-driven Design in PHP - PHP Benelux 2018
cassell
1
200
php[world] 2017 Domain-Driven Design Workshop
cassell
1
370
Domain-Driven Design
cassell
2
1k
Other Decks in Programming
See All in Programming
セグメントとターゲットを意識するプロポーザルの書き方 〜採択の鍵は、誰に刺すかを見極めるマーケティング戦略にある〜
m3m0r7
PRO
0
640
JOAI2026 1st solution - heron0519 -
heron0519
0
160
CDK Deployのための ”反響定位”
watany
5
900
The Monolith Strikes Back: Why AI Agents ❤️ Rails Monoliths
serradura
0
370
AWSコミュニティ活動は顧客のクラウド推進に効くのか / Do AWS community activities help customers adopt the cloud?
seike460
PRO
0
150
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
190
Cache-moi si tu peux : patterns et pièges du cache en production - Devoxx France 2026 - Conférence
slecache
0
320
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
130
属人化しないコード品質の作り方_2026.04.07.pdf
muraaano
0
270
Road to RubyKaigi: Play Hard(ware)
makicamel
1
500
UIの境界線をデザインする | React Tokyo #15 メイントーク
sasagar
2
400
PHP で mp3 プレイヤーを実装しよう
m3m0r7
PRO
0
290
Featured
See All Featured
Exploring anti-patterns in Rails
aemeredith
3
340
Testing 201, or: Great Expectations
jmmastey
46
8.1k
The SEO Collaboration Effect
kristinabergwall1
1
430
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
2.9k
Are puppies a ranking factor?
jonoalderson
1
3.3k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
140
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
160
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
540
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
160
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
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