value - initial. Note the argument order — the bound amount is what gets removed, not the starting point. Throws InvalidArgumentException on non-number input.
Creates a Closure that subtracts a pre-defined amount from any number passed to it. The bound value is the amount to remove — the passed value is the operand. Result is `value - initial`.
/**
* @param int|float $initial Defaults to 0. The amount to subtract.
* @return Closure(int|float):(int|float)
* @throws InvalidArgumentException If $initial is not int or float.
*/
Numbers\subtract($initial = 0): ClosureWhen Numbers\subtract() 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 subtracts 10 from whatever it receives.
$lessTen = Numbers\subtract(10);
// Called as a function.
echo $lessTen(25); // 15
echo $lessTen(3); // -7
// Used in a higher order function.
$array = array_map($lessTen, [10, 20, 30]);
print_r($array); // [0, 10, 20]This can be called inline using currying.
echo Numbers\subtract(10)(25); // 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\subtract(1), [10, 20, 30]);
print_r($array); // [9, 19, 29]