srph/compose

Composing function calls

Installs: 13

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/srph/compose

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

This package is not auto-updated.

Last update: 2025-10-16 00:08:58 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.