Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PHP, Arrays & Functional Programming

511 views

Published on

An approach in PHP to wrap arrays with a class to normalize array functions, giving them ObjectOriented and Functional Programming abilities.

Published in: Technology
  • Be the first to comment

PHP, Arrays & Functional Programming

  1. 1. PHP, Arrays & Functional Programming
  2. 2. 4/2016 Model Driven Software Development Sclable transforms your knowledge into enterprise-grade, ready-to-go Business Applications. Sclable Business Solutions GmbH https://sclable.com/
  3. 3. 4/2016 By the way: We’re always looking for Full Stack Developers
  4. 4. 4/2016 Sclable Platform Senior Developer PL/PGSQL - PHP - JS ← full stack → Aviation Enthusiast Michael Rutz
  5. 5. 4/2016 f(x) === f(x) Immutable y = x; f(x); x === y; No side effects Functional Programming
  6. 6. 4/2016 $first = 0; $second = 1; for ($i = 0; $i < 10; $i++) { echo $first . PHP_EOL; $tmp = $first; $first = $second; $second += $tmp; } Functional Programming Global state has changed
  7. 7. 4/2016 function fibonacci($n, $first = 0, $second = 1) { if ($n === 0) return ''; return $first . PHP_EOL . fibonacci(--$n, $second, $first + $second); } echo fibonacci(10); Functional Programming Global state untouched Immutable
  8. 8. 4/2016 FPHP? Functional Programming in PHP
  9. 9. 4/2016 Imperative design, but Closures ✓ Functional Programming in PHP
  10. 10. 4/2016 $ids = array_map(function ($item) { return $item->id; }, $list); // js / ES2015 let ids = list.map(item => item.id); Functional Programming in PHP
  11. 11. 4/2016 Implemented the right way, code readability can be improved. Functional Programming in PHP
  12. 12. 4/2016 https://secure.php.net/manual/en/book.array.php Array Functions
  13. 13. 4/2016 Performance ! Nice shorthands ! Array Functions
  14. 14. 4/2016 $idsToLoad = array_diff($idsRequired, $idsLoaded); // e.g. cities -> countries $referencedCountryIds = array_unique([1,1,2,3]); Array Functions
  15. 15. 4/2016 array array_map(callable, array, array...) bool array_walk(&array, callable) array array_unique(array, sort_flags) Inconsistent API
  16. 16. 4/2016 https://github.com/sclable/array-functions composer require sclable/array-functions class ArrayWrap
  17. 17. 4/2016 Normalized Params OO & FP Approach class ArrayWrap
  18. 18. 4/2016 // e.g. cities -> countries $referencedCountryIds = ArrayWrap::create($cities) ->map(function ($city) { return->countryId; }) ->unique(); e.g.
  19. 19. 4/2016 More e.g.
  20. 20. 4/2016 $array = array_pad([], 10, 0); foreach ($array as &$item) { $item = rand(); } // find max $max = null; foreach ($array as $item) { $max = $max === null ? $item : max($max, $item); }
  21. 21. 4/2016 ArrayWrap::create([]) ->pad(10, 0) ->map(function () { return rand(); }) ->max();
  22. 22. 4/2016 // [x] immutable // [x] no side effects $emptyArr = ArrayWrap::create([]); $padded = $emptyArr->pad(10, 0); $randList = $padded->map( … ); $max = $randList->max(); var_dump($emptyArr === $padded); // false var_dump($padded === $randList); // false
  23. 23. 4/2016 foreach vs. array_map/array_walk Performance Considerations
  24. 24. 4/2016 ! major performance impact on closures ! xDebug
  25. 25. 4/2016 $ids = [] foreach ($models as $model) { $ids[] = $model->id; } $ids = array_map( function ($model) { return $model->id; }, $models ); Extract a list of ids
  26. 26. 4/2016 Foreach vs. Array_map 1:0 Extract a list of ids
  27. 27. 4/2016 $assigned = []; foreach ($instances as $instance) { $assigned[$instance->id] = $instance; } $assigned = array_combine(array_map( function ($instance) {return $instance->id;}, $instances ), $instances); Assign by id
  28. 28. 4/2016 Foreach vs. Array_map 2:0 Assign by id
  29. 29. 4/2016 $yelled = [‘OH’, ‘SO’, ‘LOUD’]; $pssst = []; foreach ($yelled as $v) { $pssst[] = strtolower($v); } $pssst = array_map(‘strtolower’, $yelled); Apply native functions
  30. 30. 4/2016 Foreach vs. Array_map 2:1 Apply native functions
  31. 31. 4/2016 Performance. But readability! Conclusions
  32. 32. 4/2016 Performance. But readability! Conclusions + It looks cool!
  33. 33. 4/2016 Thank you. That’s all folks!

×