49
By Luis Atencio (@luijar) PHP = PHunctional Programming Northeast PHP Conference 2016

PHP = PHunctional Programming

Embed Size (px)

Citation preview

Page 1: PHP = PHunctional Programming

By Luis Atencio (@luijar)

PHP = PHunctional Programming

Northeast PHP Conference 2016

Page 2: PHP = PHunctional Programming

What? Functional... PHP?

Page 3: PHP = PHunctional Programming

You don’t need to understand Category Theory to grok FP

Page 4: PHP = PHunctional Programming

Agenda

1. Understand the FP stuff2. Function composition and declarative

coding3. Currying4. Functors5. Monads6. Cool FP libs for PHP

Page 5: PHP = PHunctional Programming
Page 6: PHP = PHunctional Programming

PHP functions became instances of Closure

Page 7: PHP = PHunctional Programming

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

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

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

Pure FunctionsA function without side effects that returns the same result on same input

Page 11: PHP = PHunctional Programming

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

Higher-order functions• Pass functions to other functions• Return functions from other functions• Lazy evaluation

Page 13: PHP = PHunctional Programming

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

Composition vs Inheritance

Page 15: PHP = PHunctional Programming

Composition vs Inheritance2

Adding new behav-ior is easy

Page 16: PHP = PHunctional Programming

Functions are first-class in PHPThe Closure class was added in PHP 5.3.0

Page 17: PHP = PHunctional Programming

Everything is an object under the hood

Syntactic Sugar

Page 18: PHP = PHunctional Programming

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

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

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

FP vs OO

Declarative vs Imperative

Functions vs ObjectsStateless vs Stateful

Mutable vs Immutable

Page 22: PHP = PHunctional Programming

Declarative• Describe WHAT a program does• Not HOW to do it

Unix pipe-line

SQL

Page 23: PHP = PHunctional Programming

Declarative2

Using PRamda Functional PHP Library

https://github.com/kapolos/pramda

Page 24: PHP = PHunctional Programming

Lambda ExpressionsArrow functions RFC under discussion

https://wiki.php.net/rfc/arrow_functions

Page 25: PHP = PHunctional Programming

Function compositionDeclarative style + modularity

Page 26: PHP = PHunctional Programming

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

Function composition2

Examples:

This is where the term “Composer” comes from

Page 28: PHP = PHunctional Programming

Function composition3

It’s just the inverse of PIPE!

Point-free style!

Page 29: PHP = PHunctional Programming

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

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

Currying3

Example:

Page 32: PHP = PHunctional Programming

Currying + CompositionExample:

Page 33: PHP = PHunctional Programming

What about errors?

Page 34: PHP = PHunctional Programming

Map-able container

Page 35: PHP = PHunctional Programming

Containers are not new

Generaliza-tion

Page 36: PHP = PHunctional Programming

Protecting access to data

?

Page 37: PHP = PHunctional Programming

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

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

Option Type2

What to do with nested types?

Introducing flatMap()

This is known as a Monad!!

Page 40: PHP = PHunctional Programming

Reactive Programming

Page 41: PHP = PHunctional Programming

The Observable typeTreat any type of data (of any size) as a composable stream. Makesuse of the Observer pattern

Page 42: PHP = PHunctional Programming

More observables

BusinessLogic

Orchestrate

Page 43: PHP = PHunctional Programming

Functional, Async computing

https://github.com/ReactiveX/RxPHP

Page 44: PHP = PHunctional Programming

Conclusion

PHP = FP + OO

“..FP in the small... OO in the large...” - Michael Feathers

Page 45: PHP = PHunctional Programming

Resources

Page 46: PHP = PHunctional Programming

Functional PHP

Resource https://leanpub.com/functional-php

Free to read!!!

Page 47: PHP = PHunctional Programming

Functional Programming in JavaScript

Resource https://www.manning.com/atencio

Get on Amazon

Page 48: PHP = PHunctional Programming

RxJS in Action

Resource https://www.manning.com/books /rxjs-in-action

Get on Manning.com

Page 49: PHP = PHunctional Programming

@luijar