Programming in handstand
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Programming in handstand

  • 723 views
Uploaded on

An example demonstrating how to have fun while learning new concepts in the art of software development.

An example demonstrating how to have fun while learning new concepts in the art of software development.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
723
On Slideshare
693
From Embeds
30
Number of Embeds
1

Actions

Shares
Downloads
2
Comments
0
Likes
2

Embeds 30

https://twitter.com 30

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Programming in handstand PHP Meetup, 2014. 04. 23.
  • 2. Programming in handstand Attila Magyar (@athoshun) http://athoshun.eu 2/149
  • 3. Programming in handstand Attila Magyar (@athoshun) http://athoshun.eu http://balabit.com 3/149
  • 4. Programming in handstand Attila Magyar (@athoshun) http://athoshun.eu http://balabit.com Rafael Ördög (@devillsroom) http://c0de-x.com http://leanpoker.org 4/149
  • 5. Programming in handstand Attila Magyar (@athoshun) http://athoshun.eu http://balabit.com Rafael Ördög (@devillsroom) http://c0de-x.com http://leanpoker.org http://coderetreat.org http://www.meetup.com/Coderetreat-Budapest/ 5/149
  • 6. Programming in handstand Attila Magyar (@athoshun) http://athoshun.eu http://balabit.com Rafael Ördög (@devillsroom) http://c0de-x.com http://leanpoker.org http://coderetreat.org http://www.meetup.com/Coderetreat-Budapest/ Gergely Nagy (@algernoone) http://asylum.madhouse-project.org/ http://syslog-ng.org/ 6/149
  • 7. FizzBuzz  Write a program that iterates through the numbers from 1 to 100 and:  For multiples of 3 print "Fizz"  For multiples of 5 print "Buzz"  For numbers which are multiples of both 3 and 5 print "FizzBuzz"  For others, print the number itself 7/149
  • 8. FizzBuzz  Write a program that iterates through the numbers from 1 to 100 and:  For multiples of 3 print "Fizz"  For multiples of 5 print "Buzz"  For numbers which are multiples of both 3 and 5 print "FizzBuzz"  For others, print the number itself 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, FizzBuzz, ... 8/149
  • 9. FizzBuzz for ($i = 0; $i < 100; ++$i) { if ($i % 3 === 0) { print "Fizz"; } if ($i % 5 === 0) { print "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { print $i; } print ", "; } 9/149
  • 10. FizzBuzz for ($i = 0; $i < 100; ++$i) { if ($i % 3 === 0) { print "Fizz"; } if ($i % 5 === 0) { print "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { print $i; } print ", "; } 10/149
  • 11. FizzBuzz function fizzbuzz() { $sequence = array(); for ($i = 0; $i < 100; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 11/149
  • 12. FizzBuzz function fizzbuzz() { $sequence = array(); for ($i = 0; $i < 100; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 12/149
  • 13. FizzBuzz function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 13/149
  • 14. FizzBuzz function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($i % 3 !== 0 && $i % 5 !== 0) { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 14/149
  • 15. FizzBuzz function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 15/149
  • 16. FizzBuzzBeep function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 16/149
  • 17. FizzBuzzBeep function fizzbuzz($with_beep = false) { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) $formatted .= "Fizz"; if ($i % 5 === 0) $formatted .= "Buzz"; if ($with_beep && $i % 7 === 0) $formatted .= "Beep"; if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 17/149
  • 18. FizzBuzzBeep function fizzbuzz($with_beep = false, $with_digits = false) { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0 || ($with_digits && strpos...)) $formatted .= "Fizz"; if ($i % 5 === 0 || ($with_digits && strpos...)) $formatted .= "Buzz"; if ($with_beep && $i % 7 === 0 || ($with_digits ...)) $formatted .= "Beep"; if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 18/149
  • 19. FizzBuzzBeep function fizzbuzz($with_beep = false, $with_digits = false) { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0 || ($with_digits && strpos...)) $formatted .= "Fizz"; if ($i % 5 === 0 || ($with_digits && strpos...)) $formatted .= "Buzz"; if ($with_beep && $i % 7 === 0 || ($with_digits ...)) $formatted .= "Beep"; if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 19/149
  • 20. No loops! 20/149
  • 21. No loops! 21/149
  • 22. No loops! function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 22/149
  • 23. No loops! function fizzbuzz($i = 1, array $result = array()) { if ($i > 100) return $result; $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted .= "$i"; } return fizzbuzz( $i + 1, array_merge($result, array($formatted)) ); } 23/149
  • 24. No loops! function fizzbuzz($i = 1, array $result = array()) { if ($i > 100) return $result; $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted .= "$i"; } return fizzbuzz( $i + 1, array_merge($result, array($formatted)) ); } PHP Fatal error:  Maximum function nesting level of '100' Reached, aborting! 24/149
  • 25. No loops! function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } 25/149
  • 26. array_map($callable, $array) function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } array_map( function ($x) { return 2*$x; }, array(1, 2, 3) ); // array(2, 4, 6) 26/149
  • 27. No loops! function fizzbuzz() { $sequence = array(); for ($i = 1; $i < 101; ++$i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } $sequence[] = $formatted; } return $sequence; } array_map( function ($x) { return 2*$x; }, array(1, 2, 3) ); // array(2, 4, 6) 27/149
  • 28. No loops! function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } return $formatted; }, range(1, 100) ); } array_map( function ($x) { return 2*$x; }, array(1, 2, 3) ); // array(2, 4, 6) 28/149
  • 29. No loops! function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } return $formatted; }, range(1, 100) ); } 29/149
  • 30. Still complex, rigid function fizzbuzz($with_beep = false, $with_digits = false) { return array_map( function ($i) use ($with_beep, $with_digits) { $formatted = ""; if ($i % 3 === 0 || ($with_digits && strpos...)) $formatted .= "Fizz"; if ($i % 5 === 0 || ($with_digits && strpos...)) $formatted .= "Buzz"; if ($with_beep && (...)) $formatted .= "Buzz"; if ($formatted === "") $formatted = "$i"; return $formatted; }, range(1, 100) ); } 30/149
  • 31. No conditional statements! function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } return $formatted; }, range(1, 100) ); } 31/149
  • 32. Create some duplication function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } return $formatted; }, range(1, 100) ); } 32/149
  • 33. Create some duplication function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted = "$i"; } return $formatted; }, range(1, 100) ); } 33/149
  • 34. Uniformize function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted .= "$i"; } return $formatted; }, range(1, 100) ); } 34/149
  • 35. Scalar vector→ function fizzbuzz() { return array_map( function ($i) { $formatted = ""; if ($i % 3 === 0) { $formatted .= "Fizz"; } if ($i % 5 === 0) { $formatted .= "Buzz"; } if ($formatted === "") { $formatted .= "$i"; } return $formatted; }, range(1, 100) ); } 35/149
  • 36. Scalar vector→ function fizzbuzz() { return array_map( function ($i) { $formatted = array(); if ($i % 3 === 0) { $formatted[] = "Fizz"; } if ($i % 5 === 0) { $formatted[] = "Buzz"; } if (count($formatted) === 0) { $formatted[] = "$i"; } return implode($formatted); }, range(1, 100) ); } 36/149
  • 37. Extract condition function fizzbuzz() { return array_map( function ($i) { $formatted = array(); if ($i % 3 === 0) { $formatted[] = "Fizz"; } if ($i % 5 === 0) { $formatted[] = "Buzz"; } if (count($formatted) === 0) { $formatted[] = "$i"; } return implode($formatted); }, range(1, 100) ); } 37/149
  • 38. Extract condition function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $condition = $i % 3 === 0; if ($condition) { $formatted[] = "Fizz"; } $condition = $i % 5 === 0; if ($condition) { $formatted[] = "Buzz"; } $condition = count($formatted) === 0; if ($condition) { $formatted[] = "$i"; } return implode($formatted); }, range(1, 100) ); } 38/149
  • 39. Extract representation function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $condition = $i % 3 === 0; if ($condition) { $formatted[] = "Fizz"; } $condition = $i % 5 === 0; if ($condition) { $formatted[] = "Buzz"; } $condition = count($formatted) === 0; if ($condition) { $formatted[] = "$i"; } return implode($formatted); }, range(1, 100) ); } 39/149
  • 40. Extract representation function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $condition = $i % 3 === 0; $representation = "Fizz"; if ($condition) $formatted[] = $representation; $condition = $i % 5 === 0; $representation = "Buzz"; if ($condition) $formatted[] = $representation; $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 40/149
  • 41. Eliminate duplication function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $condition = $i % 3 === 0; $representation = "Fizz"; if ($condition) $formatted[] = $representation; $condition = $i % 5 === 0; $representation = "Buzz"; if ($condition) $formatted[] = $representation; $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 41/149
  • 42. Eliminate duplication function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $condition = $i % 3 === 0; $representation = "Fizz"; if ($condition) $formatted[] = $representation; $condition = $i % 5 === 0; $representation = "Buzz"; if ($condition) $formatted[] = $representation; $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 42/149
  • 43. Eliminate duplication function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")); foreach ($rules as $rule) { list($condition, $representation) = $rule; if ($condition) $formatted[] = $representation; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 43/149
  • 44. array_filter($array, $callable) function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")); foreach ($rules as $rule) { list($condition, $representation) = $rule; if ($condition) $formatted[] = $representation; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } array_filter(array(1, 2, 3, 4), function ($x) { return $x > 2; }); // array(3, 4) 44/149
  • 45. Eliminate conditional function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")); foreach ($rules as $rule) { list($condition, $representation) = $rule; if ($condition) $formatted[] = $representation; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 45/149
  • 46. Eliminate conditional function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array_filter( array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")), function (array $rule) { return $rule[0]; }); foreach ($rules as $rule) { list($condition, $representation) = $rule; $formatted[] = $representation; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 46/149
  • 47. Remove some noise function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array_filter( array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")), function (array $rule) { return $rule[0]; }); foreach ($rules as $rule) { list($condition, $representation) = $rule; $formatted[] = $representation; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 47/149
  • 48. Remove some noise function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array_filter( array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")), function (array $rule) { return $rule[0]; }); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 48/149
  • 49. Remove some noise function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array_filter( array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")), function (array $rule) { return $rule[0]; }); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 49/149
  • 50. Zoom in function fizzbuzz() { return array_map( function ($i) { $formatted = array(); $rules = array_filter( array(array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz")), function (array $rule) { return $rule[0]; }); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) $formatted[] = $representation; return implode($formatted); }, range(1, 100) ); } 50/149
  • 51. Zoom in $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) { $formatted[] = $representation; } return implode($formatted); 51/149
  • 52. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($formatted) === 0; $representation = "$i"; if ($condition) { $formatted[] = $representation; } return implode($formatted); 52/149
  • 53. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($rules) === 0; $representation = "$i"; if ($condition) { $formatted[] = $representation; } return implode($formatted); 53/149
  • 54. Uniformize $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $condition = count($rules) === 0; $representation = "$i"; if ($condition) { $formatted[] = $representation; } return implode($formatted); 54/149
  • 55. Uniformize $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $rule = array(count($rules) === 0, "$i"); if ($rule[0]) { $formatted[] = $rule[1]; } return implode($formatted); 55/149
  • 56. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } $rule = array(count($rules) === 0, "$i"); if ($rule[0]) { $formatted[] = $rule[1]; } return implode($formatted); 56/149
  • 57. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } if (count($rules) === 0) { $rules[] = array(count($rules) === 0, "$i"); } return implode($formatted); 57/149
  • 58. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } if (count($rules) === 0) { $rules[] = array(count($rules) === 0, "$i"); } return implode($formatted); 58/149
  • 59. $formatted $rules→ $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(count($rules) === 0, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 59/149
  • 60. Remove some redundancy $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(count($rules) === 0, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 60/149
  • 61. Remove some redundancy $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(true, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 61/149
  • 62. array_pad($array, $size, $value) $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(true, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); array_pad(array(1, 2), 5, 42); // array(1, 2, 42, 42, 42) array_pad(array(1, 2), 1, 42); // array(1, 2) 62/149
  • 63. array_pad(array(1, 2), 5, 42); // array(1, 2, 42, 42, 42) array_pad(array(1, 2), 1, 42); // array(1, 2) Padding $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(true, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 63/149
  • 64. Padding $formatted = array(); $rules = array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ); if (count($rules) === 0) { $rules[] = array(true, "$i"); } foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 64/149
  • 65. Padding $formatted = array(); $rules = array_pad(array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 65/149
  • 66. Padding $formatted = array(); $rules = array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 66/149
  • 67. foreach array_map()→ $formatted = array(); $rules = array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ); foreach ($rules as $rule) { $formatted[] = $rule[1]; } return implode($formatted); 67/149
  • 68. foreach array_map()→ $formatted = array_map(function(array $rule){return $rule[1];}, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") )); return implode($formatted); 68/149
  • 69. foreach array_map→ $formatted = array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) ); return implode($formatted); 69/149
  • 70. inline implode $formatted = array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) ); return implode($formatted); 70/149
  • 71. inline implode return implode(array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) )); 71/149
  • 72. inline implode return implode( array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) ) ); 72/149
  • 73. Eliminate unneeded values return implode( array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) ) ); 73/149
  • 74. array_pad() array_map()↔ return implode( array_map( function (array $rule) { return $rule[1]; }, array_pad( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), 1, array(true, "$i") ) ) ); 74/149
  • 75. array_pad() array_map()↔ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 75/149
  • 76. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 76/149
  • 77. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 77/149
  • 78. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 78/149
  • 79. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 79/149
  • 80. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 80/149
  • 81. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 81/149
  • 82. array_map() for_all()→ return implode( array_pad( array_map( function (array $rule) { return $rule[1]; }, array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ) ), 1, "$i" ) ); 82/149
  • 83. array_map() for_all()→ function for_all(array $items, $action) { return array_map($action, $items); } return implode( array_pad( for_all( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), function (array $rule) { return $rule[1]; } ), 1, "$i" ) ); 83/149
  • 84. array_filter matching_rules→ function for_all(array $items, $action) { return array_map($action, $items); } return implode( array_pad( for_all( array_filter( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ), function (array $rule) { return $rule[0]; } ), function (array $rule) { return $rule[1]; } ), 1, "$i" ) ); 84/149
  • 85. array_filter matching_rules→ function matching_rules(array $rules) { return array_filter( $rules, function (array $rule) { return $rule[0]; }); } return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), function (array $rule) { return $rule[1]; } ), 1, "$i" ) ); 85/149
  • 86. For all matching rules... function matching_rules(array $rules) { return array_filter( $rules, function (array $rule) { return $rule[0]; }); } return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), function (array $rule) { return $rule[1]; } ), 1, "$i" ) ); 86/149
  • 87. select representation return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), function (array $rule) { return $rule[1]; } ), 1, "$i" ) ); 87/149
  • 88. select representation $select_representation = function (array $rule) { return $rule[1]; }; return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), $select_representation ), 1, "$i" ) ); 88/149
  • 89. select representation function select_representation() { return function (array $rule) { return $rule[1]; }; } return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), 1, "$i" ) ); 89/149
  • 90. array_pad() either_or_else()→ function select_representation() { return function (array $rule) { return $rule[1]; }; } return implode( array_pad( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), 1, "$i" ) ); 90/149
  • 91. array_pad() either_or_else()→ function either_or_else(array $either, $or_else) { return array_pad($either, 1, $or_else); } return implode( either_or_else( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), "$i" ) ); 91/149
  • 92. implode() concat()→ function either_or_else(array $either, $or_else) { return array_pad($either, 1, $or_else); } return implode( either_or_else( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), "$i" ) ); 92/149
  • 93. implode() concat()→ function concat(array $parts) { return implode($parts); } return concat( either_or_else( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), "$i" ) ); 93/149
  • 94. Parametric rules: conditions function concat(array $parts) { return implode($parts); } return concat( either_or_else( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), "$i" ) ); 94/149
  • 95. Parametric rules: conditions function matching_rules(array $rules) { return array_filter( $rules, function (array $rule) { return $rule[0]; }); } return concat( either_or_else( for_all( matching_rules( array( array($i % 3 === 0, "Fizz"), array($i % 5 === 0, "Buzz"), ) ), select_representation() ), "$i" ) ); 95/149
  • 96. Parametric rules: conditions function matching_rules($n, array $rules) { return array_filter( $rules, function (array $rule) use ($n) {return $rule[0]($n);});} return concat( either_or_else( for_all( matching_rules( $i, array( array(function ($n) { return $n%3 === 0; }, "Fizz"), array(function ($n) { return $n%5 === 0; }, "Buzz"), ) ), select_representation() ), "$i" ) ); 96/149
  • 97. Remove duplication function matching_rules($n, array $rules) { return array_filter( $rules, function (array $rule) use ($n) {return $rule[0]($n);});} return concat( either_or_else( for_all( matching_rules( $i, array( array(function ($n) { return $n%3 === 0; }, "Fizz"), array(function ($n) { return $n%5 === 0; }, "Buzz"), ) ), select_representation() ), "$i" ) ); 97/149
  • 98. Remove duplication function is_multiple_of($divisor) { return function ($n) use ($divisor) { return $n % $divisor === 0; }; } return concat( either_or_else( for_all( matching_rules( $i, array( array(is_multiple_of(3), "Fizz"), array(is_multiple_of(5), "Buzz"), ) ), select_representation() ), "$i" ) ); 98/149
  • 99. Parametric representation function is_multiple_of($divisor) { return function ($n) use ($divisor) { return $n % $divisor === 0; }; } return concat( either_or_else( for_all( matching_rules( $i, array( array(is_multiple_of(3), "Fizz"), array(is_multiple_of(5), "Buzz"), ) ), select_representation() ), "$i" ) ); 99/149
  • 100. Parametric representation return concat( either_or_else( for_all( matching_rules( $i, array( array(is_multiple_of(3), function ($n) { return "Fizz"; }), array(is_multiple_of(5), function ($n) { return "Buzz"; }), ) ), select_representation() ), function ($n) { return "$n"; } ) ); 100/149
  • 101. Parametric representation return concat(for_all( either_or_else( for_all( matching_rules( $i, array( array(is_multiple_of(3), function ($n) { return "Fizz"; }), array(is_multiple_of(5), function ($n) { return "Buzz"; }), ) ), select_representation() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } )); 101/149
  • 102. Parametric representation return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_representation() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } ) ); 102/149
  • 103. Parametric action function select_representation() { return function (array $rule) { return $rule[1]; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_representation() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } ) ); 103/149
  • 104. Parametric action function select_action() { return function (array $rule) { return $rule[1]; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } ) ); 104/149
  • 105. Parametric action function select_action() { return function (array $rule) { return $rule[1]; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } ) ); 105/149
  • 106. Parametric action return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), function ($f) use ($i) { return $f($i); } ) ); 106/149
  • 107. Parametric action function apply_with($n) { return function ($f) use ($n) { return $f($n); }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), apply_with($i) ) ); 107/149
  • 108. Parametric action function apply_with($n) { return function ($f) use ($n) { return $f($n); }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), apply_with($i) ) ); 108/149
  • 109. Remove duplication return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), function ($n){return "Fizz";}), array(is_multiple_of(5), function ($n){return "Buzz";}),) ), select_action() ), function ($n) { return "$n"; } ), apply_with($i) ) ); 109/149
  • 110. Remove duplication function const_fn($const) { return function ($n) use ($const) { return $const; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), const_fn("Fizz")), array(is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), function ($n) { return "$n"; } ), apply_with($i) ) ); 110/149
  • 111. Name things function const_fn($const) { return function ($n) use ($const) { return $const; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), const_fn("Fizz")), array(is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), function ($n) { return "$n"; } ), apply_with($i) ) ); 111/149
  • 112. Name things function to_string() { return function ($n) { return "$n"; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), const_fn("Fizz")), array(is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), to_string() ), apply_with($i) ) ); 112/149
  • 113. Encapsulation function to_string() { return function ($n) { return "$n"; }; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(array(is_multiple_of(3), const_fn("Fizz")), array(is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), to_string() ), apply_with($i) ) ); 113/149
  • 114. Encapsulation function when($condition, $action) { return array($condition, $action); } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(when( is_multiple_of(3), const_fn("Fizz")), when( is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), to_string() ), apply_with($i) ) ); 114/149
  • 115. When ... then ... function when($condition, $action) { return array($condition, $action); } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(when( is_multiple_of(3), const_fn("Fizz")), when( is_multiple_of(5), const_fn("Buzz")),) ), select_action() ), to_string() ), apply_with($i) ) ); 115/149
  • 116. When ... then ... function then($action) { return $action; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(when( is_multiple_of(3), then(const_fn("Fizz"))), when( is_multiple_of(5), then(const_fn("Buzz"))),) ), select_action() ), to_string() ), apply_with($i) ) ); 116/149
  • 117. Extract ugly details function then($action) { return $action; } return concat( for_all( either_or_else( for_all( matching_rules( $i, array(when( is_multiple_of(3), then(const_fn("Fizz"))), when( is_multiple_of(5), then(const_fn("Buzz"))),) ), select_action() ), to_string() ), apply_with($i) ) ); 117/149
  • 118. Extract ugly details function apply_rules($n, array $rules, $otherwise_fn) { return concat( for_all( either_or_else( for_all(matching_rules($n, $rules), select_action()), $otherwise_fn ), apply_with($n) ) ); } return apply_rules( $i, array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ); 118/149
  • 119. Extract ugly details function apply_rules($n, array $rules, $otherwise_fn) { return concat( for_all( either_or_else( for_all(matching_rules($n, $rules), select_action()), $otherwise_fn ), apply_with($n) ) ); } return apply_rules( $i, array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ); 119/149
  • 120. Extract ugly details function fizzbuzz() { return for_all( range(1, 100), function ($i) { apply_rules( $i, array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ) } ); } 120/149
  • 121. Extract ugly details function fizzbuzz() { return for_all( range(1, 100), function ($i) { apply_rules( $i, array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ) } ); } 121/149
  • 122. Extract ugly details function apply_rules(array $rules, $otherwise_fn) { return function ($n) use ($rules, $otherwise_fn) { return concat(for_all( ... )); }; } function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ) ); } 122/149
  • 123. Readability refinements function apply_rules(array $rules, $otherwise_fn) { return function ($n) use ($rules, $otherwise_fn) { return concat(for_all( ... )); }; } function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), to_string() ) ); } 123/149
  • 124. Readability refinements function otherwise($action) { return $action; } function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), otherwise(to_string()) ) ); } 124/149
  • 125. Readability refinements function otherwise($action) { return $action; } function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), otherwise(to_string()) ) ); } 125/149
  • 126. Readability refinements function otherwise($action) { return $action; } function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), otherwise(convert_to_string()) ) ); } 126/149
  • 127. Extract, extract, extract function fizzbuzz() { return for_all( range(1, 100), apply_rules( array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ), otherwise(convert_to_string()) ) ); } 127/149
  • 128. Extract, extract, extract function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string())));} function fizzbuzz() { return array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ) ; } 128/149
  • 129. The resulting algorithm function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz() { return array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ); } 129/149
  • 130. The resulting algorithm function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz() { return array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ); } 130/149
  • 131. The resulting algorithm function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 131/149
  • 132. The resulting algorithm function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return array( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))), ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 132/149
  • 133. The resulting algorithm function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } function rules() { return func_get_args(); } 133/149
  • 134. SOLID: Open/Closed Principle function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } 134/149
  • 135. SOLID: Open/Closed Principle function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzzbeep_rules() { return append( fizzbuzz_rules(), when(is_multiple_of(7), then(const_fn("Beep"))) ); } 135/149
  • 136. SOLID: Open/Closed Principle function format_numbers(array $rules) { return for_all(range(1, 100), apply_rules($rules, otherwise(convert_to_string()))); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzzbeep_rules() { return append( fizzbuzz_rules(), when(is_multiple_of(7), then(const_fn("Beep"))) ); } function append(array $array, $item) { ... } 136/149
  • 137. SOLID: What about the others? function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 137/149
  • 138. SOLID: Single Responsibility function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 138/149
  • 139. SOLID: Single Responsibility ✓ function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 139/149
  • 140. SOLID: Liskov Substitution function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 140/149
  • 141. SOLID: Liskov Substitution ✓ function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 141/149
  • 142. SOLID: Interface Segregation function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 142/149
  • 143. SOLID: Interface Segregation ✓ function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 143/149
  • 144. SOLID: Dependency Inversion function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 144/149
  • 145. SOLID: Dependency Inversion ✓ function format_numbers(array $rules) { return for_all( range(1, 100), apply_rules( $rules, otherwise(convert_to_string()) ) ); } function fizzbuzz_rules() { return rules( when(is_multiple_of(3), then(const_fn("Fizz"))), when(is_multiple_of(5), then(const_fn("Buzz"))) ); } function fizzbuzz() { return format_numbers(fizzbuzz_rules()); } 145/149
  • 146. Wait a sec: isn't SOLID for OOP? 146/149
  • 147. Wait a sec: isn't SOLID for OOP?  If you have OO, you can have FP:  Iterator Lazy evaluation→  Iterator, Command Map, Filter→  Command, Factory High order functions→  Immutability  Composition Over Inheritance 147/149
  • 148. Get out of your comfort zone  And extend your mental toolbox!
  • 149. Thank you!