An implementation of the autoCurry function from wu.js for PHP
PHP
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpunit.xml.dist

README.md

autoCurry

Build Status

Enables automatic currying of functions. Every call to an autoCurryed function will return a new function accepting the remaining function parameters until all parameters have been gathered whereupon the wrapped function will be executed and return its value.

Note that the function not only works on other functions, it also works on objects implementing __invoke(), closures, static methods and standard object methods.

To see how autoCurry can help you write very terse code, watch this video: Hey underscore, you're doing it wrong!

Usage

$add = m4rw3r\autoCurry(function($a, $b) {
	return $a + $b;
});

$add5 = $add(5);

var_dump($add5(3)); /* int(8) */

$concat = m4rw3r\autoCurry(function($a, $b) {
	return $a . $b;
});

$namePrefix = $concat('Test');

$names = array_map($namePrefix, ['Foo', 'Bar']);
/* array(2) { [0] => string(7) "TestFoo" [1] => string(7) "TestBar" } */

Prototype

callable m4rw3r\autoCurry(callable [, num_params = false])
  • callable: The function/closure/method/object to curry
  • num_params: Sets the limit for the auto-curry, useful in cases where optional parameters are present or when a method accepting any number of parameters is being curryed.

autoCurry() will automatically determine the number of parameters if num_params is false, through the use of ReflectionMethod and ReflectionFunction.