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

    View full-size slide

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

    View full-size slide

  3. Named Functions

    View full-size slide

  4. Named Functions

    View full-size slide

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

    View full-size slide

  6. Lambda Functions

    View full-size slide

  7. Lambda Functions

    View full-size slide

  8. Lambda Functions

    View full-size slide

  9. Type Hinting

    View full-size slide

  10. Wow
    much useful
    such amaze
    !
    very
    programmer

    View full-size slide

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

    View full-size slide

  12. Anonymous Functions

    View full-size slide

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

    View full-size slide

  14. λ
    Closures
    Rasmus’s Throne Room

    View full-size slide

  15. Lambda Variable Scope

    View full-size slide

  16. Closure Variable Scope & “use”

    View full-size slide

  17. Passing a Variable to a Lambda or Closure

    View full-size slide

  18. Passing a Variable by Reference to a Lambda or Closure

    View full-size slide

  19. Closure Class

    View full-size slide

  20. Closure!
    {!
    ! /* Methods */!
    ! public bindTo () { }!
    ! !
    ! !
    !
    !
    !
    }
    PHP Closure Class
    !
    ! !
    ! public static bind () {}!
    !
    ! ** double-secret function __invoke() { }!
    !

    View full-size slide

  21. Reasons to use Closures
    • Variable Scope and Binding!
    • Don’t Pollute Namespace With
    Functions!
    • Map/Reduce/Filter!
    • Data Output!
    • Retrieving Records from
    Database

    View full-size slide

  22. • Currying!
    • Fixed-point Combinators (Y-
    Combinator)!
    • Improved Performance
    (Memoization)!
    • Monads!
    • Dependency Injection
    Reasons to use Closures

    View full-size slide

  23. Things to Avoid
    • create_function!
    • Closure::bind()

    View full-size slide

  24. Map/Reduce/Filter

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  28. Database Records

    View full-size slide

  29. Y-Combinator

    View full-size slide

  30. Y-Combinator

    View full-size slide

  31. Y-Combinator

    View full-size slide

  32. 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

    View full-size slide

  33. 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

    View full-size slide

  34. MonadPHP
    Anthony Ferrara

    View full-size slide

  35. Monads Credit: Anthony Ferrara

    View full-size slide

  36. Monads Credit: Anthony Ferrara

    View full-size slide

  37. Monads Credit: Anthony Ferrara

    View full-size slide

  38. $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

    View full-size slide

  39. Monads Credit: Anthony Ferrara

    View full-size slide

  40. Dependency!
    Injection

    View full-size slide

  41. Dependency Injection

    View full-size slide

  42. Dependency Injection

    View full-size slide

  43. Dependency Injection

    View full-size slide

  44. Dependency Injection

    View full-size slide

  45. Dependency Injection

    View full-size slide