php = phunctional programming
TRANSCRIPT
![Page 1: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/1.jpg)
By Luis Atencio (@luijar)
PHP = PHunctional Programming
Northeast PHP Conference 2016
![Page 2: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/2.jpg)
What? Functional... PHP?
![Page 3: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/3.jpg)
You don’t need to understand Category Theory to grok FP
![Page 4: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/4.jpg)
Agenda
1. Understand the FP stuff2. Function composition and declarative
coding3. Currying4. Functors5. Monads6. Cool FP libs for PHP
![Page 5: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/5.jpg)
![Page 6: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/6.jpg)
PHP functions became instances of Closure
![Page 7: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/7.jpg)
Mathematical-like computing
ƛx.x + 1
The birth of the anonymous function!
(ƛx.x + 1)3 = 4
(ƛx.x + 1)((ƛy.y + 2))3 = 6
![Page 8: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/8.jpg)
What did we learn from the Maths?Pure Functions• No side effects!• Clear contracts input and output• Self-documenting• StatelessImmutability• Can’t change the contents of something once it’s been initial-
izedHigher-order functions• Pass functions into other functions• Return functions from other functions
![Page 9: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/9.jpg)
What is a side effect?A function execution that impacts its outer scope. The effect is moredetrimental when it’s the global scope
![Page 10: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/10.jpg)
Pure FunctionsA function without side effects that returns the same result on same input
![Page 11: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/11.jpg)
Immutability• Look at immutability through the consistent (repeatable) exe-
cution of pure functions• Most bugs originate from data issues• Functions are (represent) immutable values
• Very limited support in PHP
![Page 12: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/12.jpg)
Higher-order functions• Pass functions to other functions• Return functions from other functions• Lazy evaluation
![Page 13: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/13.jpg)
Variable functions• If a variable has parenthesis appended to it, PHP will try to
invoke it• Can be passed in by name (string) or reference• Will not work with language constructs:
• echo• print• unset• isset• empty
![Page 14: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/14.jpg)
Composition vs Inheritance
![Page 15: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/15.jpg)
Composition vs Inheritance2
Adding new behav-ior is easy
![Page 16: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/16.jpg)
Functions are first-class in PHPThe Closure class was added in PHP 5.3.0
![Page 17: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/17.jpg)
Everything is an object under the hood
Syntactic Sugar
![Page 18: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/18.jpg)
Towards more functional codePHP 4.0.6 introduced (but they are not pure!) to eliminate loop-ing!• array_map()
• array_filter()• array_reduce()
![Page 19: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/19.jpg)
Application State: OOIn OO you move state by passing objects around and invoking methods on those objects. In FP you pass state to the functions and combine them to move/transform it.
Ob-jectA
Ob-jectC
Ob-jectB
ObjectD
![Page 20: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/20.jpg)
Application State: FPIn FP you pass state to the functions and combine them to move/transform it.
FuncAFuncC
FuncB
FuncD
![Page 21: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/21.jpg)
FP vs OO
Declarative vs Imperative
Functions vs ObjectsStateless vs Stateful
Mutable vs Immutable
![Page 22: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/22.jpg)
Declarative• Describe WHAT a program does• Not HOW to do it
Unix pipe-line
SQL
![Page 23: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/23.jpg)
Declarative2
Using PRamda Functional PHP Library
https://github.com/kapolos/pramda
![Page 24: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/24.jpg)
Lambda ExpressionsArrow functions RFC under discussion
https://wiki.php.net/rfc/arrow_functions
![Page 25: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/25.jpg)
Function compositionDeclarative style + modularity
![Page 26: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/26.jpg)
Function compositionThe composition of functions f and g is another function that takes the result of g and pass it to f:
f o g = f( g(x) )
f g Input: AOutput: CA -> BB -> C
B
![Page 27: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/27.jpg)
Function composition2
Examples:
This is where the term “Composer” comes from
![Page 28: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/28.jpg)
Function composition3
It’s just the inverse of PIPE!
Point-free style!
![Page 29: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/29.jpg)
Currying• Composition requires we use single argument functions• Partially evaluate a function as a sequence of steps providing
one argument at a time• PHP uses eager function evaluation
function f(a, b) { … }
f aevaluating: returns:
( )
![Page 30: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/30.jpg)
Currying2
Transforms a multi-argument function into several single-arity functionsfunction f(a, b) { … }
curry(f) = function (a, b) {return function (a) {
return function (b) { ...
} } }
wait to be invoked
![Page 31: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/31.jpg)
Currying3
Example:
![Page 32: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/32.jpg)
Currying + CompositionExample:
![Page 33: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/33.jpg)
What about errors?
![Page 34: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/34.jpg)
Map-able container
![Page 35: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/35.jpg)
Containers are not new
Generaliza-tion
![Page 36: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/36.jpg)
Protecting access to data
?
![Page 37: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/37.jpg)
Mapping a value to a container
strtolower
HELLO
HELLO Map function
hello
wrapContainer
HELLO
Lift This is known as a Func-tor!!
![Page 38: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/38.jpg)
Option TypeIntended for cases when you might want to return a value—like an object, or (optionally) not—like null.
https://github.com/schmittjoh/php-option
vs
![Page 39: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/39.jpg)
Option Type2
What to do with nested types?
Introducing flatMap()
This is known as a Monad!!
![Page 40: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/40.jpg)
Reactive Programming
![Page 41: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/41.jpg)
The Observable typeTreat any type of data (of any size) as a composable stream. Makesuse of the Observer pattern
![Page 42: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/42.jpg)
More observables
BusinessLogic
Orchestrate
![Page 43: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/43.jpg)
Functional, Async computing
https://github.com/ReactiveX/RxPHP
![Page 44: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/44.jpg)
Conclusion
PHP = FP + OO
“..FP in the small... OO in the large...” - Michael Feathers
![Page 45: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/45.jpg)
Resources
![Page 46: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/46.jpg)
Functional PHP
Resource https://leanpub.com/functional-php
Free to read!!!
![Page 47: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/47.jpg)
Functional Programming in JavaScript
Resource https://www.manning.com/atencio
Get on Amazon
![Page 48: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/48.jpg)
RxJS in Action
Resource https://www.manning.com/books /rxjs-in-action
Get on Manning.com
![Page 49: PHP = PHunctional Programming](https://reader035.vdocuments.mx/reader035/viewer/2022062400/587fd2d11a28ab58248b5025/html5/thumbnails/49.jpg)
@luijar