PHP = PHunctional Programming

Preview:

Citation preview

By Luis Atencio (@luijar)

PHP = PHunctional Programming

Northeast PHP Conference 2016

What? Functional... PHP?

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

Agenda

1. Understand the FP stuff2. Function composition and declarative

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

PHP functions became instances of Closure

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

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

What is a side effect?A function execution that impacts its outer scope. The effect is moredetrimental when it’s the global scope

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

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

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

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

Composition vs Inheritance

Composition vs Inheritance2

Adding new behav-ior is easy

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

Everything is an object under the hood

Syntactic Sugar

Towards more functional codePHP 4.0.6 introduced (but they are not pure!) to eliminate loop-ing!• array_map()

• array_filter()• array_reduce()

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

Application State: FPIn FP you pass state to the functions and combine them to move/transform it.

FuncAFuncC

FuncB

FuncD

FP vs OO

Declarative vs Imperative

Functions vs ObjectsStateless vs Stateful

Mutable vs Immutable

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

Unix pipe-line

SQL

Declarative2

Using PRamda Functional PHP Library

https://github.com/kapolos/pramda

Lambda ExpressionsArrow functions RFC under discussion

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

Function compositionDeclarative style + modularity

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

Function composition2

Examples:

This is where the term “Composer” comes from

Function composition3

It’s just the inverse of PIPE!

Point-free style!

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:

( )

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

Currying3

Example:

Currying + CompositionExample:

What about errors?

Map-able container

Containers are not new

Generaliza-tion

Protecting access to data

?

Mapping a value to a container

strtolower

HELLO

HELLO Map function

hello

wrapContainer

HELLO

Lift This is known as a Func-tor!!

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

Option Type2

What to do with nested types?

Introducing flatMap()

This is known as a Monad!!

Reactive Programming

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

More observables

BusinessLogic

Orchestrate

Functional, Async computing

https://github.com/ReactiveX/RxPHP

Conclusion

PHP = FP + OO

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

Resources

Functional PHP

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

Free to read!!!

Functional Programming in JavaScript

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

Get on Amazon

RxJS in Action

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

Get on Manning.com

@luijar