Slide 1

Slide 1 text

functional approach in software design Tomasz Kowalczyk @tmmx

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

introduction

Slide 5

Slide 5 text

purpose

Slide 6

Slide 6 text

levels

Slide 7

Slide 7 text

level 0: approach

Slide 8

Slide 8 text

paradigm shift

Slide 9

Slide 9 text

SOLID

Slide 10

Slide 10 text

immutability

Slide 11

Slide 11 text

higher order functions

Slide 12

Slide 12 text

composition

Slide 13

Slide 13 text

level 1: operations

Slide 14

Slide 14 text

casual conditionals

Slide 15

Slide 15 text

$result = []; foreach($this->users as $user) { if($user->isDisabled()) { continue; } if($user->isHidden()) { continue; } $result[] = $user; } return $result;

Slide 16

Slide 16 text

$result = []; foreach($this->users as $user) { if($user->isDisabled()) { continue; } if($user->isHidden()) { continue; } $result[] = $user; } return $result;

Slide 17

Slide 17 text

$isEnabled = function(User $u): bool { return false === $u->isDisabled(); }; $isVisible = function(User $u): bool { return false === $u->isHidden(); }; return array_filter( array_filter($this->users, $isVisible), $isEnabled);

Slide 18

Slide 18 text

$isEnabled = function(User $u): bool { return false === $u->isDisabled(); }; $isVisible = function(User $u): bool { return false === $u->isHidden(); }; return $this->users ->filter($isVisible) ->filter($isEnabled);

Slide 19

Slide 19 text

endless loops

Slide 20

Slide 20 text

$ids = []; foreach($this->users as $user) { foreach($user->getTags() as $tag) { $tagId = $tag->getId(); if(false === in_array($tagId, $ids, true)) { $ids[] = $tagId; } } } return $ids;

Slide 21

Slide 21 text

$ids = []; foreach($this->users as $user) { foreach($user->getTags() as $tag) { $tagId = $tag->getId(); if(false === in_array($tagId, $ids, true)) { $ids[] = $tagId; } } } return $ids;

Slide 22

Slide 22 text

$userToTags = function(User $user): array { return $user->getTags(); }; $tagToId = function(Tag $tag): int { return $tag->getId(); }; return array_unique(array_map($tagToId, array_merge( ...array_map($userToTags, $this->users) )));

Slide 23

Slide 23 text

$userToTags = function(User $user): array { return $user->getTags(); }; $tagToId = function(Tag $tag): int { return $tag->getId(); }; return $this->users ->flatMap($userToTags) ->map($tagToId) ->unique();

Slide 24

Slide 24 text

accidental accumulators

Slide 25

Slide 25 text

$points = 0; foreach($this->users as $user) { $points += $user->getPoints(); } return $points;

Slide 26

Slide 26 text

$points = 0; foreach($this->users as $user) { $points += $user->getPoints(); } return $points;

Slide 27

Slide 27 text

$addPoints = function(int $state, User $user): int { return $state + $user->getPoints(); }; return array_reduce($this->users, $addPoints, 0);

Slide 28

Slide 28 text

$addPoints = function(int $state, User $user): int { return $state + $user->getPoints(); }; return $this->users ->reduce($addPoints, 0);

Slide 29

Slide 29 text

combined

Slide 30

Slide 30 text

$isEnabled = function(User $u): bool { return !$u->isDisabled(); }; $isVisible = function(User $user): bool { return !$u->isHidden(); }; $userToPoints = function(User $user): int { return $user->getPoints(); }; $addPoints = function(int $st, User $u): int { return $st + $u->getPoints(); }; return $this->users ->filter($isEnabled) ->filter($isVisible) ->map($userToPoints) ->reduce($addPoints, 0);

Slide 31

Slide 31 text

$isEnabled = function(User $u): bool { return !$u->isDisabled(); }; $isVisible = function(User $user): bool { return !$u->isHidden(); }; $userToPoints = function(User $user): int { return $user->getPoints(); }; $addPoints = function(int $st, User $u): int { return $st + $u->getPoints(); }; return $this->users ->filter($isEnabled) ->filter($isVisible) ->map($userToPoints) ->reduce($addPoints, 0);

Slide 32

Slide 32 text

level 2: components

Slide 33

Slide 33 text

request lifecycle

Slide 34

Slide 34 text

entry | input → request routing | request → route controller | controller → response events | request → request | response → response forms | data → mapping validation | data → violations

Slide 35

Slide 35 text

$response = controller(route($request));

Slide 36

Slide 36 text

object graph serialization

Slide 37

Slide 37 text

objects → normalization → formatting → data data → parsing → hydration → objects

Slide 38

Slide 38 text

$json = format(normalize($user)) $user = hydrate(parse($json))

Slide 39

Slide 39 text

$normalizers = new NormalizerContainer(); $normalizers->add(User::class, function(User $u) { return [ 'id' => $u->getId(), 'email' => $u->getEmail(), ]; });

Slide 40

Slide 40 text

$hydrators = new HydratorContainer(); $hydrators->add(User::class, function(array $data) { return new User($data['id'], $data['email']); });

Slide 41

Slide 41 text

$formats = new FormatContainer(); $formats->add('json', new JsonFormat());

Slide 42

Slide 42 text

$s = new Serializard($normalizers, $hydrators, $formats); $user = new User(1337, '[email protected]'); $json = $s->serialize($user, 'json'); $user = $s->unserialize($json, 'json', User::class);

Slide 43

Slide 43 text

shortcode processing

Slide 44

Slide 44 text

This is a [b]bold text[/b]. This is a bold text.

Slide 45

Slide 45 text

string → parse → process → replace → string

Slide 46

Slide 46 text

$string = replace(process(parse($input)))

Slide 47

Slide 47 text

$handlers = new HandlerContainer(); $handlers->add('b', function(ShortcodeInterface $s) { return ''.$s->getContent().''; });

Slide 48

Slide 48 text

$parser = new RegexParser(); $parser = new WordpressParser(); $parser = new RegularParser();

Slide 49

Slide 49 text

$processor = new Processor($parser, $handlers); $text = 'This is a [b]bold text[/b].'; $result = $processor->process($text);

Slide 50

Slide 50 text

level 3: systems

Slide 51

Slide 51 text

modelling data flow

Slide 52

Slide 52 text

applications microservices APIs

Slide 53

Slide 53 text

challenges

Slide 54

Slide 54 text

purpose

Slide 55

Slide 55 text

comfort zone

Slide 56

Slide 56 text

"real world"

Slide 57

Slide 57 text

performance

Slide 58

Slide 58 text

summary

Slide 59

Slide 59 text

Slides: https://speakerdeck.com/thunderer/functional-approach-in-software-design Resources: https://github.com/thunderer/Shortcode https://github.com/thunderer/Serializard https://en.wikipedia.org/wiki/Functional_programming http://www.phptherightway.com/pages/Functional-Programming.html https://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming https://pragprog.com/magazines/2012-08/functional-thinking-for-the-imperative-mind http://lambdaconf.us/downloads/documents/lambdaconf_slfp.pdf Images: https://www.flickr.com/photos/kinematic/4078206226 (rocks) https://www.flickr.com/photos/kyetis/12506278675 (stones) https://www.flickr.com/photos/skyseeker/14404947216 (lightning)

Slide 60

Slide 60 text

questions?

Slide 61

Slide 61 text

how can I help? github /thunderer twitter @tmmx

Slide 62

Slide 62 text

please rate the talk and leave feedback joind.in/talk/c0b77

Slide 63

Slide 63 text

thanks! github /thunderer twitter @tmmx