Slide 1

Slide 1 text

FUNCTIONAL PROGRAMMING Basic concepts you can start using today Thiago Colares, 2021-05-14. Montréal, Québec, Canada.

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

1. What is functional programming? 3

Slide 4

Slide 4 text

What is functional programming? ▪ It's a programming paradigm. ▪ Treat functions as mathematical ones: pure functions. ▪ No hidden IOs; avoid state and side effects. ▪ No pure functional programming language is needed. 4

Slide 5

Slide 5 text

A function in Math It's a mapping from a set of inputs x to a set of outputs y. 1. Each input x is mapped to exactly one and only one y. 2. y depends solely and exclusively on x. 5

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

7 1. The output depends only on the input. 2. No side effects. It does NOT: - modify the global state; - launch a missile; - modify the input; etc... A code function as in Math: Pure Functions.

Slide 8

Slide 8 text

2. Benefits 8

Slide 9

Slide 9 text

What are the benefits? ▪ Fewer side effects → fewer bugs ▪ Output only depends on arguments → easier tests ▪ Inputs and outputs are explicit → easier maintenance ▪ More reuse and composition → faster coding 9

Slide 10

Slide 10 text

3. Some simple examples 10

Slide 11

Slide 11 text

Fewer side effects. Fewer bugs. class Calendar { public $defaultInterval = "P1D"; // ... function addOneDay($date) { return $date->add(new DateInterval($this->defaultInterval)); } } 11 class Calendar { // ... function addOneDay($date) { return (clone $date)->add(new DateInterval("P1D")); } } $now = new DateTime; // 2021-05-05 // ... $nowAddedInOneDay = $calendar->addOneDay($now); // $nowAddedInOneDay: 2021-05-06. Fine! // $now: 2021-05-06. Wait, waat?

Slide 12

Slide 12 text

Output only depends on arguments. Easier tests. 12 class Calendar { public $defaultInterval = "P1D"; // ... function addOneDay($date) { return $date->add(new DateInterval($this->defaultInterval)); } } class Calendar { // ... function addOneDay($date) { return $date->add(new DateInterval("P1D")); } } $calendar->defaultInterval = 'P2D'; // oops! //... $now = new DateTime; // Value: 2021-05-05. $nowAddedInOneDay = $calendar->addOneDay($now); // Result: 2021-05-07. Expected: 2021-05-06.

Slide 13

Slide 13 text

Inputs and outputs are explicit. Easier maintenance. 13 class Calendar { public $defaultInterval = "P1D"; // ... function addOneDay($date) { return $date->add(new DateInterval($this->defaultInterval)); } } class Calendar { // ... function addOneDay($date) { return $date->add(new DateInterval("P1D")); } } class Calendar { // ... function addInterval($date, $durationIso) { return $date->add(new DateInterval($durationIso)); } } $calendar->defaultInterval = 'P2D'; // oops! //... $now = new DateTime; // Value: 2021-05-05. $nowAddedInOneDay = $calendar->addOneDay($now); // Result: 2021-05-07. Expected: 2021-05-06.

Slide 14

Slide 14 text

Avoid state. Less room for unexpected behaviors. 14

Slide 15

Slide 15 text

public function render($request, \Exception $exception) { $code = null; // ... switch (true) { case $exception instanceof ApplicationException: $code = Response::HTTP_BAD_REQUEST; // ... case $exception instanceof NotFoundHttpException: $code = Response::HTTP_NOT_FOUND; // ... // ... 82 lines } return response()->json( // ... $code ); } 15 What if something else modifies $code in between?

Slide 16

Slide 16 text

public function render($request, \Exception $exception) { $code = null; // ... switch (true) { case $exception instanceof ApplicationException: $code = Response::HTTP_BAD_REQUEST; // ... case $exception instanceof NotFoundHttpException: $code = Response::HTTP_NOT_FOUND; // ... // ... 82 lines } return response()->json( // ... $code ); } 16 public function render($request, \Exception $exception) { //... if ($exception instanceof ApplicationException) { return response()->json(//..., Response::HTTP_BAD_REQUEST); } //... if ($exception instanceof NotFoundHttpException) { return response()->json(//..., Response::HTTP_NOT_FOUND); } // ... 82 lines } No state, no aux vars needed here.

Slide 17

Slide 17 text

4. Closing words 17

Slide 18

Slide 18 text

This is the tip of the iceberg 18 Referential transparency Lambda expressions Monads Immutability Recursion map(), reduce(), filter() Parametric polymorphism Currying Closures Functors

Slide 19

Slide 19 text

Conclusions ▪ Pure functions: no state, no side effects, explicit IOs. ▪ Fewer side effects → fewer bugs ▪ Output only depends on arguments → easier tests ▪ Inputs and outputs are explicit → easier maintenance ▪ More reuse and composition → faster coding ▪ There are no silver bullets. Use them when it makes sense. 19

Slide 20

Slide 20 text

“ Explicit is better than implicit. Flat is better than nested. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. The Zen of Python. 20

Slide 21

Slide 21 text

Thanks ;) 21