Numbers\remainderBy()

transformer returns Closure pure throws
number → (number → float)
At a glance — Bind a divisor; the returned Closure yields the modulus of any number. Throws InvalidArgumentException on non-number input.

Creates a Closure that returns the remainder when dividing the passed value by a pre-defined divisor. Result is `value % divisor`. Use `remainderInto()` 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\remainderBy($divisor = 1): Closure

Returned Closure

When Numbers\remainderBy() 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 gets the remainder after division by 2.
$modTwo = Numbers\remainderBy(2);

// Called as a function.
echo $modTwo(10); // 0
echo $modTwo(9);  // 1

// Used in a higher order function.
$array = array_map($modTwo, [10, 11, 12, 13, 14]);
print_r($array); // [0, 1, 0, 1, 0]

Curried

This can be called inline using currying.

echo Numbers\remainderBy(3)(10); // 1

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\remainderBy(5), [10, 11, 12, 13, 14]);
print_r($array); // [0, 1, 2, 3, 4]

Details

Numbers Functions

Releated Number arithmetic Functions