Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Obtaining Closure with Anonymous Functions

Obtaining Closure with Anonymous Functions

PHP Functional Programming using Functions, Lambdas, Anonymous Functions, and Closures.

Andrew Cassell

May 22, 2014
Tweet

More Decks by Andrew Cassell

Other Decks in Programming

Transcript

  1. Obtaining Closure with Anonymous Functions @alc277

  2. Functions&! Lambdas&! Anonymous&! Closures&! Fun.

  3. Named Functions

  4. Named Functions

  5. Functions&! Lambdas&! Anonymous&! Closures&! Fun.

  6. NAMED LAMBDA

  7. Lambda Functions

  8. None
  9. Lambda Functions

  10. Lambda Functions

  11. Type Hinting

  12. Wow much useful such amaze ! very programmer

  13. Functions&! Lambdas&! Anonymous&! Closures&! Fun.

  14. None
  15. Anonymous Functions

  16. Functions&! Lambdas&! Anonymous&! Closures&! Fun.

  17. λ Closures Rasmus’s Throne Room

  18. None
  19. Lambda Variable Scope

  20. Closure Variable Scope & “use”

  21. Tricky Bit

  22. Passing a Variable to a Lambda or Closure

  23. None
  24. Passing a Variable by Reference to a Lambda or Closure

  25. None
  26. None
  27. Closure Class

  28. Closure! {! ! /* Methods */! ! public bindTo ()

    { }! ! ! ! ! ! ! ! } PHP Closure Class ! ! ! ! public static bind () {}! ! ! ** double-secret function __invoke() { }! !
  29. None
  30. Reasons to use Closures • Variable Scope and Binding! •

    Don’t Pollute Namespace With Functions! • Map/Reduce/Filter! • Data Output! • Retrieving Records from Database
  31. • Currying! • Fixed-point Combinators (Y- Combinator)! • Improved Performance

    (Memoization)! • Monads! • Dependency Injection Reasons to use Closures
  32. Things to Avoid • create_function! • Closure::bind()

  33. Map/Reduce/Filter

  34. Map/Reduce/Filter with Lambda Credit: Fabien Potencier

  35. Map/Reduce/Filter Credit: Fabien Potencier Map/Reduce/Filter with Closure

  36. Currying Photo: Penn State Office of Research Communications.

  37. Currying

  38. DATA OUTPUT

  39. Data Output

  40. Data Output

  41. Database Records

  42. Data Output

  43. Y-Combinator

  44. Y-Combinator

  45. Y-Combinator

  46. None
  47. Memoization

  48. fib(5) = fib(3) + fib(4)! fib(3) = fib(1) + fib(2)!

    fib(1) = 1! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(3) = 1 + 1 = 2! fib(4) = fib(3) + fib(2)! fib(3) = fib(1) + fib(2)! fib(1) = 1! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(3) = 1 + 1 = 2! fib(2) = fib(1) + fib(0)! fib(1) = 1! fib(0) = 0! fib(2) = 1 + 0 = 1! fib(4) = 2 + 1 = 3! fib(5) = 3 + 2 = 5
  49. Memoization

  50. 1 1 2 1 3 2 4 3 5 5

    6 8 7 13 8 21 9 34 10 55 11 89 12 144 13 233 14 377 15 610 16 987
  51. Memoization

  52. None
  53. Monads

  54. MonadPHP Anthony Ferrara

  55. Monads Credit: Anthony Ferrara

  56. Monads Credit: Anthony Ferrara

  57. Monads Credit: Anthony Ferrara

  58. $posts = [! [“title" => "foo", "author" => [“name" =>

    "Bob", "email" => “[email protected]”]],! [“title" => "bar", "author" => array("name" => "Tom", "email" => “[email protected]”]],! [“title" => “baz"],! [“title" => "biz", "author" => array("name" => "Mark", "email" => “[email protected]”]],! ]; Monads Credit: Anthony Ferrara
  59. Monads Credit: Anthony Ferrara

  60. Dependency! Injection

  61. Dependency Injection

  62. Dependency Injection

  63. Dependency Injection

  64. Dependency Injection

  65. Dependency Injection