Skip to content

srph/php-compose

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

compose Build Status Latest Stable Version Total Downloads Latest Unstable Version License

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.

About

Composing function calls in PHP

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages