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. Closure! {! ! /* Methods */! ! public bindTo ()

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

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

    (Memoization)! • Monads! • Dependency Injection Reasons to use Closures
  4. 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
  5. 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
  6. $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