-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathCurryN.php
More file actions
45 lines (39 loc) · 1.36 KB
/
Copy pathCurryN.php
File metadata and controls
45 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
/**
* @package Functional-php
* @author Lars Strojny <lstrojny@php.net>
* @copyright 2011-2021 Lars Strojny
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/lstrojny/functional-php
*/
namespace Functional;
/**
* Return a version of the given function where the $count first arguments are curried.
*
* No check is made to verify that the given argument count is either too low or too high.
* If you give a smaller number you will have an error when calling the given function. If
* you give a higher number, arguments will simply be ignored.
*
* @template R
* @template V
*
* @param int $count number of arguments you want to curry
* @param callable(V...):R $function the function you want to curry
*
* @return callable(V...):R a curried version of the given function
*
* @no-named-arguments
*/
function curry_n($count, callable $function)
{
$accumulator = function (array $arguments) use ($count, $function, &$accumulator) {
return function (...$newArguments) use ($count, $function, $arguments, $accumulator) {
$arguments = \array_merge($arguments, $newArguments);
if ($count <= \count($arguments)) {
return \call_user_func_array($function, $arguments);
}
return $accumulator($arguments);
};
};
return $accumulator([]);
}