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
530
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
45
Immutability to Save an Ever-Changing World
cassell
0
20
Domain-driven Design Workshop
cassell
1
260
Immutability to save an ever-changing world
cassell
3
490
Introduction to Domain-Driven Design in PHP - ZendCon & OpenEnterprise 2018
cassell
3
730
Immutability to Save an Ever-Changing World - PHP Benelux 2018
cassell
1
650
Domain-driven Design in PHP - PHP Benelux 2018
cassell
1
160
php[world] 2017 Domain-Driven Design Workshop
cassell
1
300
Domain-Driven Design
cassell
2
900
Other Decks in Programming
See All in Programming
Devin入門と最近のアップデートから見るDevinの進化 / Introduction to Devin and the Evolution of Devin as Seen in Recent Update
rkaga
9
4.5k
AHC 044 混合整数計画ソルバー解法
kiri8128
0
320
「影響が少ない」を自分の目でみてみる
o0h
PRO
2
900
OpenTelemetryを活用したObservability入門 / Introduction to Observability with OpenTelemetry
seike460
PRO
1
420
データベースエンジニアの仕事を楽にする。PgAssistantの紹介
nnaka2992
9
4.5k
AHC045_解説
shun_pi
0
430
gen_statem - OTP's Unsung Hero
whatyouhide
1
190
地域ITコミュニティの活性化とAWSに移行してみた話
yuukis
0
220
パスキーのすべて / 20250324 iddance Lesson.5
kuralab
0
150
WordPress Playground for Developers
iambherulal
0
130
エンジニア未経験が最短で戦力になるためのTips
gokana
0
260
英語 × の私が、生成AIの力を借りて、OSSに初コントリビュートした話
personabb
0
180
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2.2k
It's Worth the Effort
3n
184
28k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
103
19k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
18
1.1k
The Cult of Friendly URLs
andyhume
78
6.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
135
33k
A Tale of Four Properties
chriscoyier
158
23k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
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" => “bob@example.com”]],! [“title" => "bar", "author" => array("name" => "Tom", "email" => “tom@example.com”]],! [“title" => “baz"],! [“title" => "biz", "author" => array("name" => "Mark", "email" => “mark@example.com”]],! ]; Monads Credit: Anthony Ferrara
Monads Credit: Anthony Ferrara
Dependency! Injection
Dependency Injection
Dependency Injection
Dependency Injection
Dependency Injection
Dependency Injection