InvalidArgumentException on non-number input.
Creates a Closure that multiplies any number it receives by a pre-defined factor. Ideal for scaling values inside map/pipe chains.
/**
* @param int|float $initial Defaults to 1.
* @return Closure(int|float):(int|float)
* @throws InvalidArgumentException If $initial is not int or float.
*/
Numbers\multiply($initial = 1): ClosureWhen Numbers\multiply() is called, it returns the following Closure which can be used like a regular function.
/**
* @param int|float $value
* @return int|float Result of ($value * $initial).
*/
$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 doubles anything it receives.
$double = Numbers\multiply(2);
// Called as a function.
echo $double(5); // 10
echo $double(2.5); // 5
// Used in a higher order function.
$array = array_map($double, [1, 2, 3, 4]);
print_r($array); // [2, 4, 6, 8]This can be called inline using currying.
echo Numbers\multiply(10)(2.5); // 25If 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\multiply(3), [1, 2, 3]);
print_r($array); // [3, 6, 9]