InvalidArgumentException on non-number input.
Creates a Closure that adds a pre-defined amount to any number passed to it. Useful wherever you want a reusable "add N" function for piping, mapping or composition.
/**
* @param int|float $initial Defaults to 0.
* @return Closure(int|float):(int|float)
* @throws InvalidArgumentException If $initial is not int or float.
*/
Numbers\sum($initial = 0): ClosureWhen Numbers\sum() is called, it returns the following Closure which can be used like a regular function.
/**
* @param int|float $value
* @return int|float
*/
$function (int|float $value): int|floatThis can be used to create a simple closure which can be used as a regular function.
// Create a function that adds 5 to whatever it receives.
$addFive = Numbers\sum(5);
// Called as a function.
echo $addFive(15.5); // 20.5
echo $addFive(-2); // 3
// Used in a higher order function.
$array = array_map($addFive, [1, 2, 3, 4]);
print_r($array); // [6, 7, 8, 9]This can be called inline using currying.
echo Numbers\sum(5)(10); // 15If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.
$array = array_map(Numbers\sum(100), [1, 2, 3]);
print_r($array); // [101, 102, 103]