Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Obtaining Closure with Anonymous Functions
Andrew Cassell
May 22, 2014
Programming
0
390
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
Domain-driven Design Workshop
cassell
1
220
Immutability to save an ever-changing world
cassell
3
300
Introduction to Domain-Driven Design in PHP - ZendCon & OpenEnterprise 2018
cassell
3
480
Immutability to Save an Ever-Changing World - PHP Benelux 2018
cassell
1
380
Domain-driven Design in PHP - PHP Benelux 2018
cassell
1
130
php[world] 2017 Domain-Driven Design Workshop
cassell
1
210
Domain-Driven Design
cassell
2
680
No Modes – The Future of all Web Applications
cassell
2
87
Domain-Driven Design - True North PHP 2016
cassell
1
230
Other Decks in Programming
See All in Programming
監視せなあかんし、五大紙だけにオオカミってな🐺🐺🐺🐺🐺
sadnessojisan
2
1.4k
xarray-Datatree: Hierarchical Data Structures for Multi-Model Science
tomnicholas
0
210
社会人 20 年目エンジニア、発信で技術学びなおしてる話
e99h2121
1
140
Git Rebase
bkuhlmann
10
1.2k
OSSから学んだPR Descriptionの書き方
fugakkbn
4
130
Hono v3 - Do Everything, Run Anywhere, But Small, And Faster
yusukebe
4
130
Circuit⚡
monaapk
0
200
爆速の日経電子版開発の今
shinyaigeek
1
540
僕が考えた超最強のKMMアプリの作り方
spbaya0141
0
180
Hasura の Relationship と権限管理
karszawa
0
160
10年以上続くプロダクトの フロントエンド刷新プロジェクトのふりかえり
yotahada3
2
320
Showkase、Paparazziを用いたビジュアルリグレッションテストの導入にチャレンジした話 / MoT TechTalk #15
mot_techtalk
0
100
Featured
See All Featured
Clear Off the Table
cherdarchuk
79
290k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
44
14k
Designing the Hi-DPI Web
ddemaree
273
32k
How GitHub Uses GitHub to Build GitHub
holman
465
280k
Build The Right Thing And Hit Your Dates
maggiecrowley
22
1.4k
Visualization
eitanlees
128
12k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
349
27k
Producing Creativity
orderedlist
PRO
335
37k
Fireside Chat
paigeccino
16
1.8k
We Have a Design System, Now What?
morganepeng
37
5.9k
Product Roadmaps are Hard
iamctodd
38
7.7k
Making the Leap to Tech Lead
cromwellryan
116
7.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