camspiers / php-fp
Functional Programming Helpers
0.2.1
2014-08-12 23:39 UTC
Requires
- php: >=5.6.0-dev
- nikic/iter: ~1.1
Requires (Dev)
- camspiers/bench: dev-master
- phpunit/phpunit: ~4.2
README
Currying
A curryable function returns a new function when called with less arguments than the curryable function requires. The new function returned will have the arguments applied, and will also be a curryable function.
This programming pattern can be used to build up more complex functions from less complex functions.
e.g.
// Create a curryable function $concat = fp\curry(function ($a, $b) { return $a . $b; }); // Create a new function with 'Mr. ' applied $addTitle = $concat('Mr. '); echo $addTitle('Spiers'); // Mr. Spiers
Composition
$h = fp\compose($f, $g);
Function composition will return a new function ($h
) which will first apply the second function ($g
), pass its
result into the first ($f
).
Usage
Turning normal functions into curryable functions
$map = fp\curry('array_map');
Create non-closure functions that are curryable
function _tag($tag, $text) { return "<$tag>$text</$tag>"; } function tag(...$args) { return fp\curry('_tag')->__invoke(...$args); } // We now have a paragraph function $p = tag('p'); // We now have a div function $div = tag('div'); echo $div($p("Some text")); // <div><p>Some text</p></div>