srph/compose

Composing function calls

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

This package is not auto-updated.

Last update: 2024-05-29 17:23: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.