php-core: bcda2e3cf8888906a10c570ba71fbd549840abc7

     1: = PHP Core Extensions =
     2: 
     3: This is a "polyfill" which provides some core language functionality not offered
     4: by PHP 'out of the box'. Includes:
     5: 
     6: == Callable Operators ==
     7: 
     8: In regular PHP, we can't treat operators as functions, eg.
     9: 
    10:     $total = array_reduce($penalties,
    11:                           function($x, $y) { return $x - $y; },
    12:                           $score);
    13:     if ($errors) { throw current($errors); }
    14: 
    15: PHP Core provides an 'op' function to work around this:
    16: 
    17:     $total = array_reduce($penalties, op('-'), $score);
    18:     array_map(op('throw'), $errors);
    19: 
    20: Core's higher-order functions apply 'op' automatically.
    21: 
    22: == Partial Application ==
    23: 
    24: By default, PHP's functions are all-or-nothing: you can either call them now,
    25: with whatever values you have, or you must wait until later:
    26: 
    27:     $r1 = func($a, $b, $c, null, null, null);
    28: 
    29:     $r2 = function($d, $e, $f) use ($a, $b, $c) {
    30:       return func($a, $b, $c, $d, $e, $f);
    31:     };
    32: 
    33: Partial application lets you pass in some arguments now and the rest later:
    34: 
    35:     $r3 = papply('func', $a, $b, $c);
    36: 
    37: == Currying ==
    38: 
    39: Curried functions don't need partial application: they collect up the right
    40: number of arguments automatically:
    41: 
    42:     $f = curry(function($w, $x, $y, $z) { return func_get_args(); });
    43:     $g = $f(10);
    44:     $h = $g(20);
    45:     $i = $h(30, 40); // $i === [10, 20, 30, 40]
    46: 
    47: They also pass surplus arguments to their return value:
    48: 
    49:     $action = curry(function($action) { return "ldap_{$action}"; });
    50:     $action('bind', $connection, $dn, $password);
    51: 
    52: Some of Core's higher-order functions will automatically curry their arguments
    53: and results.
    54: 
    55: == Named Closures ==
    56: 
    57: PHP separates functions into those which are named (AKA 'global functions') and
    58: those which are anonymous (AKA 'Closure instances'):
    59: 
    60:     function i_am_named($x) { return $x; }
    61:     $i_am_anonymous = function($x) { return $x; };
    62: 
    63: PHP allows named functions to be treated like anonymous functions:
    64: 
    65:     $i_act_anonymously = 'i_am_named';
    66: 
    67: Core allows anonymous functions to be named:
    68: 
    69:     defun('i_act_named', $i_am_anonymous);
    70: 
    71: == Extras ==
    72: 
    73: A few handy functions are provided too:
    74: 
    75:  - compose: Function composition
    76:  - call: Like call_user_func but uses op and curry
    77:  - uncurry: Like call_user_func_array but uses op and curry
    78:  - up_to: Like range but handles 0 correctly
    79:  - arity: Counts a function's arguments (handles defun, papply, op and curry)

Generated by git2html.