Numbers\subtract()

transformer returns Closure pure throws
number → (number → number)
At a glance — Bind a subtrahend; the returned Closure computes 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): Closure

Returned Closure

When 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|float

Examples

Partial Application

This 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]

Curried

This can be called inline using currying.

echo Numbers\subtract(10)(25); // 15

Inlined with Higher Order Function

If 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]

Details

Numbers Functions

Releated Number arithmetic Functions