Numbers\divideInto()

transformer returns Closure pure throws
number → (number → float)
At a glance — Bind a numerator; the returned Closure computes dividend / value. Use divideBy() for the reverse direction. Throws InvalidArgumentException on non-number input.

Creates a Closure that divides a pre-defined dividend by whatever number it receives. Result is `dividend / value`. Use this when the numerator is the constant and you want to vary the denominator — see `divideBy()` for the reverse.

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

Returned Closure

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

/**
  * @param int|float $value
  * @return float  Result of ($dividend / $value).
  */
$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 divides 12 by whatever it receives.
$twelveOver = Numbers\divideInto(12);

// Called as a function.
echo $twelveOver(4); // 3.0
echo $twelveOver(2); // 6.0

// Used in a higher order function.
$array = array_map($twelveOver, [1, 2, 3, 4]);
print_r($array); // [12.0, 6.0, 4.0, 3.0]

Curried

This can be called inline using currying.

echo Numbers\divideInto(100)(4); // 25.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\divideInto(60), [1, 2, 3, 4, 5, 6]);
print_r($array); // [60.0, 30.0, 20.0, 15.0, 12.0, 10.0]

Details

Numbers Functions

Releated Number arithmetic Functions