30秒学会 PHP 片段 · 2020年2月26日

30秒学会 PHP 片段 – compose

Return a new function that composes multiple functions into a single callable.

Use array_reduce() to perform right-to-left function composition.

代码实现

function compose(...$functions)
{
  return array_reduce(
    $functions,
    function ($carry, $function) {
      return function ($x) use ($carry, $function) {
        return $function($carry($x));
      };
    },
    function ($x) {
      return $x;
    }
  );
}

使用样例

$compose = compose(
  // add 2
  function ($x) {
    return $x + 2;
  },
  // multiply 4
  function ($x) {
    return $x * 4;
  }
);
$compose(3); // 20