Slide 1

Slide 1 text

Functional Programming in PHP

Slide 2

Slide 2 text

Lochemem Bruno Michael ● PHP enthusiast from Kampala, Uganda ● Functional Programming aficionado ● Writes PHP, JavaScript, and some C++ ● Recently graduated college ● Maintains PHP userland packages and extensions ● Authored a book ● Loves hoops, movies, and video games

Slide 3

Slide 3 text

Functional Programming, huh? ● A declarative programming paradigm ● Mandates that pure functions be written ● Saliently features the use of expressions ● Emphasizes the importance of composition

Slide 4

Slide 4 text

This is not an FP vs OOP debate!

Slide 5

Slide 5 text

What are the benefits? ● Functional Programming helps address complexity ○ Complexity often deters project advancement ○ Functions exist only as callable units and do not affect histories ● FP techniques truncate the cognitive load of developing software ○ Short-term memory is a finite resource ○ FP conditions one to parameterize constructs ○ The paradigm sturdies one’s intellection when writing programs

Slide 6

Slide 6 text

4-7 Quanta in working memory models

Slide 7

Slide 7 text

All of this in PHP?

Slide 8

Slide 8 text

Expressions

Slide 9

Slide 9 text

First-class functions

Slide 10

Slide 10 text

Do not forget everything you know about programming!

Slide 11

Slide 11 text

Functions everywhere!

Slide 12

Slide 12 text

User-defined functions

Slide 13

Slide 13 text

Anonymous functions

Slide 14

Slide 14 text

Class methods

Slide 15

Slide 15 text

Keep the functions pure, please! Side effects Side causes

Slide 16

Slide 16 text

Not so pure! ● Mutable global variable (side-cause) ● Value mutated after function is called (side-effect)

Slide 17

Slide 17 text

An improvement? ● Use a closure ● Mutate only within the function

Slide 18

Slide 18 text

Parameterize the things!

Slide 19

Slide 19 text

Another fix? ● Parameterize the accumulator ● Return one integer for every $x-$acc parameter combination

Slide 20

Slide 20 text

Every argument input should yield the same output.

Slide 21

Slide 21 text

Referential Transparency ● Is a quality of pure functions ● Dictates that pure functions calls should be substitutable with their outputs

Slide 22

Slide 22 text

What about classes?

Slide 23

Slide 23 text

OOP has a Forgotten History!

Slide 24

Slide 24 text

What we know ● Classes are Composite Data Types ● Encapsulation ● Property visibility ● Inheritance (is-a relationships) ● Polymorphism ● Setters and Getters What we may not know ● Alan Kay’s biological analogy ● Immutability in objects ● A message-passing API

Slide 25

Slide 25 text

Not pure! ● Setter mutates internal state whenever invoked ● Each setter call increases temporal coupling

Slide 26

Slide 26 text

The cardinal rule of purity still applies!

Slide 27

Slide 27 text

Either ● Remove the setter ● Single $empName value for each object

Slide 28

Slide 28 text

Or ● Make the setter a pure function ● Create new copy of Employee

Slide 29

Slide 29 text

Pure Functions ● Do not create side-effects ● Yield the same output given the same input ● Are referentially transparent

Slide 30

Slide 30 text

Compose the functions! f(x) = x + 1 g(x) = 2x f ∘ g = f(g(x))

Slide 31

Slide 31 text

“If you’re chaining, you’re composing” - From Eric Elliott in Composing Software: An Introduction

Slide 32

Slide 32 text

Traditionally ● Invoke functions as function arguments ● Cumbersome to deal with long chains

Slide 33

Slide 33 text

Approaching helper library territory!

Slide 34

Slide 34 text

bingo-functional ● Suite of FP libraries for PHP and JavaScript ● REPL for FP experimentation

Slide 35

Slide 35 text

Point-free? ● No explicit parameter mentions ● Seemingly more compact

Slide 36

Slide 36 text

const for functions?

Slide 37

Slide 37 text

Some FP sauce to add to your program ● Partial application ● Currying ● Map, Filter, Fold/Reduce ● Pattern Matching ● Persistent Data Structures

Slide 38

Slide 38 text

You might have used some of these techniques before.

Slide 39

Slide 39 text

Currying ● Decompose a function into a series of sub-functions ● Each sub-function takes one argument

Slide 40

Slide 40 text

Partial Application ● Similar to currying ● Also useful for deferred function calls ● Each sub-function has variable arity

Slide 41

Slide 41 text

Map, Filter, Fold ● Higher-order functions for array transformations ● Highly composable ● Single iteration for every transformation

Slide 42

Slide 42 text

Pattern Matching ● Native support in pure FP languages ● Useful whenever data has discernible patterns ● Especially good for flow-control ● Composable, as a function

Slide 43

Slide 43 text

Immutable Lists ● Persistent data structure ● Each transformation outputs a new copy ● Single iteration for each transformation

Slide 44

Slide 44 text

Real world IO is messy!

Slide 45

Slide 45 text

Messy, huh? Why? ● Many operations are inherently impure ● Exceptions - used commonly - are also inherently impure ● IO can get complicated

Slide 46

Slide 46 text

Impure IO? ● Filesystem interaction ● Writing to and reading from standard output and standard input ● Opening windows ● Database interactions

Slide 47

Slide 47 text

Exceptions ● Inherently impure ● Not good for function chains

Slide 48

Slide 48 text

How you can cope with this ● Use functors and monads ● Parameterize (might lead to callback hell) ● Favor composition with forward function chaining ● Use persistent data structures

Slide 49

Slide 49 text

Programming with effects...

Slide 50

Slide 50 text

Functor ● Something you can map over ● Typically a type class ● A type of category ● Supports forward chaining

Slide 51

Slide 51 text

Monad ● A special type of functor ● Convenient because of inherent ad-hoc value ● Especially good for flow-control

Slide 52

Slide 52 text

Real world monads ● Either and Maybe union types ● IO monad ● State monad ● Reader monad ● Writer monad

Slide 53

Slide 53 text

Maybe Monad ● Composed of Just and Nothing types ● Can evaluate to one of either sub-type at runtime ● Useful for error handling

Slide 54

Slide 54 text

Either Monad ● Composed of Left and Right types ● Allows for more succinct error handling ● Great for the Railway pattern

Slide 55

Slide 55 text

IO Monad ● Great for I/O-related tasks ● Encapsulated within the monad is an executable function

Slide 56

Slide 56 text

State Monad ● Stores an initial state ● Creates copies of said state; retains both

Slide 57

Slide 57 text

Reader Monad ● Useful for sharing data amongst functions ● Akin to Dependency Injection (DI) in OO

Slide 58

Slide 58 text

Writer Monad ● Great for logging data ● Can, in some contexts, be used for event sourcing

Slide 59

Slide 59 text

How do I know I’m doing it right? The functions you write should have the following qualities: ● Purity ● Referential Transparency ● Composability ● Conciseness

Slide 60

Slide 60 text

The future looks bright for FP in PHP! ● New language features ● New RFCs ● New libraries ● More explanatory material ● All-around language consistency

Slide 61

Slide 61 text

My Book Functional Programming in PHP

Slide 62

Slide 62 text

Thank you!