m4rw3r/auto-curry

Provides automatic currying of functions and methods.

dev-master / 1.0.x-dev 2013-11-18 10:23 UTC

This package is not auto-updated.

Last update: 2025-06-17 09:04:03 UTC


README

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.