Numbers\divideBy()

transformer returns Closure pure throws
number → (number → float)
At a glance — Bind a divisor; the returned Closure computes value / divisor. Always returns a float. Throws InvalidArgumentException on non-number input.

Creates a Closure that divides any number it receives by a pre-defined divisor. Result is `value / divisor` and is always returned as a float. Use `divideInto()` if you want the pre-defined value to be the dividend instead.

/**
  * @param int|float $divisor Defaults to 1. The fixed divisor.
  * @return Closure(int|float):float
  * @throws InvalidArgumentException If $divisor is not int or float.
  */
Numbers\divideBy($divisor = 1): Closure

Returned Closure

When Numbers\divideBy() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param int|float $value
  * @return float  Result of ($value / $divisor).
  */
$function (int|float $value): 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 halves every number it receives.
$half = Numbers\divideBy(2);

// Called as a function.
echo $half(10); // 5.0
echo $half(7);  // 3.5

// Used in a higher order function.
$array = array_map($half, [10, 20, 30]);
print_r($array); // [5.0, 10.0, 15.0]

Curried

This can be called inline using currying.

echo Numbers\divideBy(3)(12); // 4.0

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\divideBy(10), [100, 200, 300]);
print_r($array); // [10.0, 20.0, 30.0]

Details

Numbers Functions

Releated Number arithmetic Functions