A utility to create curried functions that its parameters are reordered by user-defined order.
Inspired by https://github.com/andjsrk/create-curried.
- PHP 8.1 or later
composer require nemorize/curried
Returns a Context
for a given closure.
use Nemorize\Curried\Curried;
$context = Curried::from(function ($foo, $bar) {
return $foo . $bar;
});
Takes a parameter at the given position.
use Nemorize\Curried\Curried;
$context = Curried::from(function ($foo, $bar) {
return $foo . $bar;
})->takes(0)->takes(1);
Takes the rest of parameters.
use Nemorize\Curried\Curried;
$context = Curried::from(function ($foo, $bar, $baz) {
return $foo . $bar . $baz;
})->takes(0)->takesRest();
Binds a parameter at the given position to the closure.
Returned closure returns a Context
for the next parameter.
use Nemorize\Curried\Curried;
$context = Curried::from(function ($foo, $bar) {
return $foo . $bar;
})->takes(0)->withStatic(1)('bar');
Generates a curried function.
use Nemorize\Curried\Curried;
$curried = Curried::from(function ($foo, $bar) {
return $foo . $bar;
})->takes(0)->takes(1)->generate();
$curried('foo')('bar'); // 'foobar'
use Nemorize\Curried\Curried;
$map = Curried::from(array_map(...))
->takes(0)
->takes(1)
->generate();
$plusOneEach = $map(fn (int $x) => $x + 1);
$plusOneEach([1, 2, 3]); // [2, 3, 4]
use Nemorize\Curried\Curried;
$sum = Curried::from(array_reduce(...))
->takes(1)
->withStatic(0)(fn (int $x, int $y) => $x + $y)
->generate();
$sum([1, 2, 3]); // 6