srph/compose

组合函数调用

v0.1.0 2015-11-07 18:55 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:36:40 UTC


README

在PHP中组合函数调用

嗯?

这是一个差异示例(之前和之后)

- $h($g($f($x)))
+ compose($h, $g, $f)(x);

更多关于函数组合 - 维基百科

安装

composer require srph/compose

支持PHP >=5.4

用法

$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

您可以传递无限数量的函数到 compose(...fn): Closure