Skip to content

An implementation of the autoCurry function from wu.js for PHP

License

Notifications You must be signed in to change notification settings

m4rw3r/autoCurry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

An implementation of the autoCurry function from wu.js for PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages