srph / compose
Composing function calls
v0.1.0
2015-11-07 18:55 UTC
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2025-03-05 20:58:22 UTC
README
Composing function calls in PHP
Huh?
Here's a diff example (before and after):
- $h($g($f($x)))
+ compose($h, $g, $f)(x);
More on Functional composition - Wikipedia.
Installing
composer require srph/compose
PHP >=5.4
is supported.
Usage
$square = function($n) {
return $n * $n;
}
$pow = function($exponent) {
return function($n) use ($exponent) {
for (;$exponent--;) {
$n .= $n;
}
return $n;
}
}
$input = 2;
$f = compose($square, $pow(3), $square);
$f($input); // 4096
You may pass an infinite number of functions to compose(...fn): Closure
.